How to Allow Only English Letters in Input Fields

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..