Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
58
rated 0 times [  59] [ 1]  / answers: 1 / hits: 18287  / 12 Years ago, thu, april 12, 2012, 12:00:00

How can I restrict users from entering special characters and numbers in the text box. I want only alphabets to be entered ( Typed / Pasted ).



give me sample javascript codes?


More From » php

 Answers
5

Rather than preventing the entry of certain characers, it is far more user friendly and effective to validate the value when the form is submitted. You really don't care what the value of the field is before that, and you don't need special handling for how the value is entered. It might be pasted, dragged, auto-entered, entered by a plugin or script, or other methods.



A trivial example:



</script>

<form onsubmit=return validate(this);>
<input type=text name=foo>
<input type=submit>
</form>

<script type=text/javascript>
function validate(form) {
var re = /^[a-z,A-Z]+$/i;

if (!re.test(form.foo.value)) {
alert('Please enter only letters from a to z');
return false;
}
}
</script>

[#86295] Tuesday, April 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondarrianb

Total Points: 48
Total Questions: 109
Total Answers: 104

Location: Zambia
Member since Thu, Jun 25, 2020
4 Years ago
;