JavaScript implements client-side validation handlers via RegExp _javascript tips

Source: Internet
Author: User
1. Let the text box only allow input numbers, the use of asp.net mvc3.0 text box control
Copy Code code 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, and the JavaScript function is triggered when you press the keyboard in a text box.
Copy Code code as follows:

<script type= "Text/javascript" >
function Regvalidateisdigit (e) {
var Keychar;
Debugger
Judge the browser
if (window.event)//ie
{
Keychar = E.keycode;
}
else if (E.which)///firefox, etc.
{
Keychar = E.which;
}
var str = string.fromcharcode (Keychar); Use Unicode encoding to find the appropriate character
return Regisdigit (str);
}
function Regisdigit (fdata) {
Define a regular match
var reg = new RegExp ("^[0-9]$");
Return (Reg.test (fdata));
}
</script>

First, judge the browser for processing compatibility. Then String.formcharcode (Keychar) to find the appropriate characters
Finally, in the function Regisdigit function
Define a regular match
Copy Code code as follows:

var reg = new RegExp ("^[0-9]$");

Because it's a value of 0-9, so it's equivalent to \d.
Copy Code code as follows:

var reg = new RegExp ("\\d$");

The direct amount of a regular expression is also defined as a character that is contained between a pair of slashes (/). Therefore, JavaScript may contain the following code:
Copy Code code as follows:

var reg=/\d$/;

The test function is also used here: Check that the specified string exists. Also commonly used are the exec match search replace split and other functions.
If you understand the first then you just need to apply the right then you can use OH.
2. The text box only allows you to enter Chinese
Copy Code code 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));
}

Regvalidateischinese ("input string") is a Chinese character returns true, not all Chinese characters return false
3. The decision of the mailbox input format
Copy Code code 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 ("is the Mailbox");
}
else {
Alert ("wrong format");
}
}

Two definitions of preliminary tests are available.
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.