I need the code using a JavaScript client valida开发者_Python百科tion to check emp id should start with character "E" with numeric code(eg:E001). Before submitting the form this validation should be done.
If you were to use FormAutoValidate (disclaimer: my library), then you would use:
<form class="autovalidate" ...>
<input type="text" name="empid" class="required" mustmatch="^E\d+$"
mustmatchmessage="Employee IDs must be of the form 'E001'.">
</form>
This is not syntactically valid HTML, but works fine in every browser.
(I do plan on switching to use HTML5 data-*
attributes for syntactic correctness under HTML5 sometime soon.)
<script language="Javascript">
function formCheck()
{
errstr = "";
// validates 'E' followed by one or more digits
if(document.empform.empid.search(/^E\d+$/)==-1)
errstr += "Invalid EMPid\n";
if(errstr!="")
{
alert(errstr);
return false;
}
return true;
}
</script>
....
<form name="empform" .... onsubmit="formCheck()" >
...
<input type="text" name="empid">
...
</form>
精彩评论