checking usn in javascript code example

Example: checking usn in javascript

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head> <title>2a.html</title> </head>
  <body>
    <h1>USN validation</h1>
    <script type="text/javascript">
    function validate(usn)
      {
          var error="";

          var pattern=/^[1-4][A-Z]{2}[0-9]{2}[A-Z]{2}[0-9]{3}$/;

          if (!usn.value.match(pattern))
             {
                error = "USN is invalid!";
             }

          if (error.length == 0)
             alert("USN is valid!");
          else
             alert(error);
      }
    </script>
    <form>
        USN: <input type="text" id="usn"/> <input type="submit" value="SUBMIT" onclick="validate(usn)"/>
    </form>
  </body>
</html>

Tags:

Misc Example