JavaScript implements client verification Handler through RegExp

Source: Internet
Author: User

1. Allow only numbers to be entered in the text box, and use the text box control of asp.net mvc3.0 Copy codeThe Code is as follows: @ Html. TextBox ("txt", null, new {@ style = "width: 300;", onkeypress = "return RegValidateIsDigit (event )"})

You can see that the onkeypress event is registered in the text box. when you enter a character in the text box and press the keyboard, the JavaScript function is triggered.Copy codeThe Code is as follows: <script type = "text/javascript">
Function RegValidateIsDigit (e ){
Var KeyChar;
Debugger;
/// Determine the browser
If (window. event) // IE
{
KeyChar = e. keyCode;
}
Else if (e. which) // FireFox, etc.
{
KeyChar = e. which;
}
Var str = String. fromCharCode (KeyChar); // you can use UniCode encoding to find the corresponding characters.
Return regIsDigit (str );
}
Function regIsDigit (fData ){
/// Define a regular expression for matching
Var reg = new RegExp ("^ [0-9] $ ");
Return (reg. test (fData ));
}
</Script>

First, judge the browser and handle the compatibility. Then use String. formCharCode (KeyChar) to find the corresponding characters
Finally, in the regIsDigit function
Define Regular Expressions for matchingCopy codeThe Code is as follows: var reg = new RegExp ("^ [0-9] $ ");

Because the value is 0-9, it is equivalent to \ d, that isCopy codeThe Code is as follows: var reg = new RegExp ("\ d $ ");

A regular expression is also defined as a character that contains a slash (/). Therefore, JavaScript may contain the following code:Copy codeThe Code is as follows: var reg =/\ d $ /;

The test function is also used to check whether the specified string exists. Common functions include exec match search replace split.
If you understand the first one, you only need to apply the regular expression.
2. Only Chinese characters can be entered in the text box. Copy codeThe Code is as follows: function RegValidateIsChinese (str ){
// Var reg = new RegExp ("^ [\ u4e00-\ u9fa5] + $ ");
Var reg =/^ [\ u4E00-\ u9FA5] + $ /;
Var str = document. getElementById ("text"). value;
Return (reg. test (str ));
}

If RegValidateIsChinese ("input string") is a Chinese character, true is returned. If it is not all Chinese characters, false is returned.
3. Determine the email input format Copy codeThe Code is as follows: function RegValidateIsEmail (str ){
// Var reg =/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + ((\. [a-zA-Z0-9 _-] {2, 3}) {1, 2}) $ /;
Var reg =/^ \ w + (-\ w +) | (\. \ w +) * @ {1} \ w + \. {1} \ w {2, 4 }(\. {0, 1} \ w {2}) {0, 1}/ig;
If (reg. test (str )){
Alert ("email ");
}
Else {
Alert ("Incorrect format ");
}
}

Both definitions can be used for preliminary testing.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.