Posted By

jprochazka on 10/02/10


Tagged

validation


Versions (?)

Validte a telephone number.


 / Published in: JavaScript
 

The following JavaScript will validate if a 10 digit phone number was supplied once the form is submitted.

  1. <head>
  2.  
  3. ...
  4.  
  5. <script language="JavaScript" type="text/javascript">
  6. function nameCheck() {
  7. }
  8. function teleCheck(teleStr) {
  9. alert(teleStr);
  10. var stripped = teleStr.replace(/[\(\)\.\-\ ]/g, '');
  11. if (teleStr == "") {
  12. alert("You must supply a phone number.");
  13. return false;
  14. } else if (isNaN(parseInt(stripped))) {
  15. alert("The supplied phone number contains illegal characters.");
  16. return false;
  17. } else if (!(stripped.length == 10)) {
  18. alert("Supplied phone number is the wrong length.\nBe sure to included an area code.");
  19. return false;
  20. }
  21. return true;
  22. }
  23. </script>
  24.  
  25. ...
  26.  
  27. </head>
  28.  
  29. ...
  30.  
  31. <body>
  32.  
  33. ...
  34.  
  35. <form id="form1" name="form1" method="post" onSubmit="return teleCheck(this.telephone.value)" action="">
  36.  
  37. ...

Report this snippet  

You need to login to post a comment.