html - Validating phone number with JavaScript -


i have form 3 elements. want validate phone number when user enters it. if user moves next element , phone number contains , characters not numbers want display alertbox.

i have written code stumped. problem having function is, if enter numbers phone number element still alert box displayed. code looks this:

<script type="text/javascript"> function validateform() { checknr= isnan(document.forms[0].elements[1]) if(checknr == true) { window.alert("you can enter numbers. please try again") } }  </script>  <form>   <strong>fullname: </strong><input type="text" / id="name"><br />   <strong>phone nr: </strong><input type="text" id="phone"  onblur="validateform()" />   <strong>nationality</strong><input type="text" id="nat" /><br />   <input type="button" id="subbutton" onclick="calc()" value="submit" /> </form> 

thank in advance answers , help.

change

document.forms[0].elements[1] 

to

document.forms[0].elements[1].value 

you testing element itself, not element's value.

jsfiddle example

btw, if enters phone number dash or parenthesis (e.g. (555) 123-4567) expect happen?


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -