How to allow only English letters in input fields?
I would try this onChange
function:
onChange={(e) => {
let value = e.target.value
value = value.replace(/[^A-Za-z]/ig, '')
this.setState({
value,
})
}}
See the codepen: https://codepen.io/bozdoz/pen/vzJgQB
The idea is to reverse your regex matcher with ^
and replace all non-A-z characters with ''
only allow English characters and numbers for text input
Assuming you also want to accept spaces:
$("#user").keypress(function(event){
var ew = event.which;
if(ew == 32)
return true;
if(48 <= ew && ew <= 57)
return true;
if(65 <= ew && ew <= 90)
return true;
if(97 <= ew && ew <= 122)
return true;
return false;
});
If you don't want to accept spaces then remove the if(ew == 32) return true;
JSFiddle
Allow only numbers and letters to input string
Would tell you may miss event parameter ?
Without jQuery works like this for me in 3 browsers:
function clsAlphaNoOnly (e) { // Accept only alpha numerics, no special characters var regex = new RegExp("^[a-zA-Z0-9 ]+$"); var str = String.fromCharCode(!e.charCode ? e.which : e.charCode); if (regex.test(str)) { return true; }
e.preventDefault(); return false;}function clsAlphaNoOnly2 () { // Accept only alpha numerics, no special characters return clsAlphaNoOnly (this.event); // window.event}
<input type="text" id="input" onkeypress="clsAlphaNoOnly(event)" onpaste="return false;"><input type="text" id="input" onkeypress="clsAlphaNoOnly2()" onpaste="return false;">
Allow only English letters without spaces or number?
You can use oninput
event with replace
to restrict input like this:
<input type="text" oninput="this.value=this.value.replace(/[^a-z]/gi,'')">
Make inputfield accept only english letters, numbers and symbols
Add a listener that changes the input field's value when it detects a change in value. And in that listener, set the value to be the same as the input but any non-compliant characters removed:
[SerializeField] InputField inputField;
void OnEnable()
{
//Register InputField Event
inputField.onValueChanged.AddListener(inputValueChanged});
}
static string CleanInput(string strIn)
{
// Replace invalid characters with empty strings.
return Regex.Replace(strIn,
@"[^a-zA-Z0-9`!@#$%^&*()_+|\-=\\{}\[\]:"";'<>?,./]", "");
}
//Called when Input changes
void inputValueChanged(string attemptedVal)
{
inputField.text = CleanInput(attemptedVal);
}
void OnDisable()
{
//Un-Register InputField Events
inputField.onValueChanged.RemoveAllListeners();
}
Sources:
- https://stackoverflow.com/a/41392130/1092820
- https://stackoverflow.com/a/14682155/1092820
Allow only english lowercase letters in input textbox
You don't need to program javascript for this task. Just use the pattern
attribute.
Try my runnable snippet. Type in a non-alphanumeric character, then click Submit. The submission will be halted.
<form><input type="text" placeholder="example.com/[your input]" pattern="[a-zA-Z0-9]+"><button type="submit">Submit</button></form>
Vue.js – How to allow an user to type only letters in an input field?
If you want the user only can type letters, then you can do v-on:keypress
and check on the event function.
<input type="text" v-model="firstName" v-on:keypress="isLetter($event)">
then the method:
isLetter(e) {
let char = String.fromCharCode(e.keyCode); // Get the character
if(/^[A-Za-z]+$/.test(char)) return true; // Match with regex
else e.preventDefault(); // If not match, don't add to input text
}
Example here: https://codesandbox.io/s/festive-buck-hm4fd?file=/src/App.vue
You can change the regex pattern to ^[A-Za-z\']+$
if you also want to allow apostrophe
I also added computed properties to check if the input value is valid or no in case you still want to allow users to input other than letters and shows error
Accepts only English characters and special characters not Unicodes
Try this..
$(document).ready(function(){ $(".english_only").on("keyup", function(){ console.log(/^[ A-Za-z_0-9@./#&+-]*$/i.test($(this).val().trim())); }); $('.english_only').on("paste",function(e) { e.preventDefault(); });});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form> <input type="text" class="english_only"/> <textarea class="english_only"></textarea></form>
Related Topics
Check If Image Exists on Server Using JavaScript
How to Create Expand/Collapse Function for Each Row of Table - Angular6
How to Close Toggle Menu When Clicking Navigation Item Anchor Link
Javascript String With New Line - But Not Using \N
Regex With Space and Letters Only
How to Partition Input Field to Appear as Separate Input Fields on Screen
How to Append Value to an Array Only If Not Exists and Remove It If Not
Check If Json Response Data Is Empty
Add Two Numbers and Display Result in Textbox With JavaScript
Regex to Find Json Values Without Quotes
React-Router Urls Don't Work When Refreshing or Writing Manually
Jquery Validation Code for Not Allowed Only Blank Space in Textbox
Filter/Search from the Drop-Down List of Options Using Angular
Print Function Not Working in Chrome
How to Get First N Number of Elements from an Array