开发者

I need the script for an emp id should start with character "E" using JavaScript?

开发者 https://www.devze.com 2023-02-04 16:19 出处:网络
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

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号