First of all, number is an at least partially reserved word, so really shouldn't be used as a name for a variable or element or anything else other than to represent the Global number object. Second, where is the value you want stored? Is it in number.value or in number?
Now I'm thinking that you don't really mean 11 numbers and no other characters, because that's not how most phone numbers are usually represented, but if you do:
Code:
function numVal(el){
if(!/^\d{11}$/.test(el.value))
alert('Enter only 11 numbers please');
}
HTML Code:
<input type="text" onchange="numVal(this);">
The test /^\d{11}$/ is a regular expression and may be altered to allow only your preferred format for the phone number, or even to allow a few various acceptable formats. For me to write that for you, I would need to know the format(s). But be careful, often the 'rules' for validation used on forms can be too constricting or too lenient, think carefully about just what you want these to be in this case.
Also, if this is being stored for later use on the server side, there should also be validation on the server side.
Bookmarks