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
Asked
Active
Viewed 545 times
2
-
It's a modular course and doesn't always focus on web design, sounds crazy I know. – Andrew Stewart May 14 '13 at 02:22
3 Answers
2
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;
}
-
-
Please don't do that. It is really annoying for users, far better to tell them what is valid, then let them get there however they want. You only care what the value is when the form is submitted, what it is before then is irrelevant. – RobG May 14 '13 at 00:29
-
depends on the situation ... everybody knows what to enter in their name. Anyway, this is just an example, I have provided just the options. – Guanxi May 14 '13 at 00:37
-
The biggest issue is that if someone accidentally types a certain wrong character, they will press delete, only to see that the character is already deleted (but only if it was not the allowed char code range) so now they've deleted two characters (sometimes). The other is that this doesn't address values that are pasted, or dragged into the textarea. It is not a good solution. – RobG May 14 '13 at 03:17
-
yes .. the situation you mentioned about draggin and pasting the text is not handeled by this. Thanks for mentioning. – Guanxi May 14 '13 at 12:56
1
var patt=/[0-9]/,
value = $('#input').val();
if(patt.test(value)) {
// Contains a number between 0-9
}

Sushanth --
- 55,259
- 9
- 66
- 105
0
if you are targetting html5 browser you can use the pattern
attribute
<input type="text" pattern="^[a-zA-Z]*$" >

Mehdi Karamosly
- 5,388
- 2
- 32
- 50