Question

I am using JavaScript on my website to check input elements within a form. I am trying to ensure that there are no numbers in the field when the user enters their name. The form is created using PHP. I am still in my first year of web design so not sure if there is a function for this. Apologies if this question has been asked, but I haven't found anything. Many Thanks

Was it helpful?

Solution 3

if you are targetting html5 browser you can use the pattern attribute

<input type="text"  pattern="^[a-zA-Z]*$" >

OTHER TIPS

You can use regular expressions or you can write a function to validate keycode.

Below is link that shows regex for alphabet with spaces. Regular Expression for alphabets with spaces

Keycode example. Put this on keypress of the textbox. This will not allow user to type characters other than alphabets. You can include other key codes for other characters you want to allow.

function onKeyPress(){
   if(event.keyCode<65 && event.keyCode>90)
     return false;
   else
     return true;
}
var patt=/[0-9]/,
    value = $('#input').val();

if(patt.test(value)) {
    // Contains a number between 0-9
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top