Common regular validation for jquery

Source: Internet
Author: User

jquery Common regular validation [go]
Basic Knowledge:
How to create a regular object in JS:
-Use the RegExp () constructor method, such as Var patten=new RegExp ("S $");
-Use regular direct volume methods such as: Var patten=/s$/;
Note: Just as a string literal is defined as a character enclosed in quotation marks (""), the direct amount of the regular expression is also defined as a character that is contained between a pair of slashes (/), and a regular global identifier at the end of the slash.

verify the number of text inputs
<script type= "Text/javascript" src= "Https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" >< /script>
<script type= "Text/javascript" >
function Strlencalc (obj, Showmeg, maxlen) {
var v = obj.val (), Charlen = 0, Curlen = maxlen, len = v.length;
for (var i = 0; i < v.length; i++) {
if (V.charcodeat (i) < 0 | | v.charcodeat (i) > 255) {
Curlen-= 1;
}
}
if (Curlen >= len) {
$ ("#" +showmeg). HTML ("can also input <strong>" +math.floor ((Curlen-len)/2) + "</strong> characters"). CSS (' color ', ');
$ ("#subBtn"). Removeattr ("Disabled");
} else {
$ ("#" +showmeg). HTML ("Already over <strong>" +math.ceil ((Len-curlen)/2) + "</strong> characters"). CSS (' color ', ' #FF0000 ') ;
$ ("#subBtn"). attr ("Disabled", "disabled");
}
}
</script>
<textarea tabindex= "One" onkeyup= "Strlencalc ($ (this), ' Showmeg ',") "Value=" "class=" px "name=" subject "id=" Subject "></textarea>
<span id= "Showmeg" > can also input <strong>40</strong> kanji </span>
<input type= "Submit" id= "Subbtn" value= "release"/>

Verify if it is a number
<script type= "Text/javascript" >
function Boolnumeric (obj)
{
var objvalue=$ (obj). val ();
if (!isnan (ObjValue)) {
Alert ("is a number!");
}
else{
Alert ("Not a number!")
}
}
</script>

Verify Mailbox
<script type= "Text/javascript" >
function Verifyemail (obj)
{
var pattern =/^[a-za-z0-9_\-]{1,}@[a-za-z0-9_\-]{1,}\. [a-za-z0-9_\-.] {1,}$/;
var objvalue=$ (obj). val ();
if (!pattern.test (ObjValue)) {
Alert ("Bad mailbox format!");
}
}
</script>

Verify phone number (phone number, landline number)
<script type= "Text/javascript" >
function Verifytelnumber (obj)
{
var pattern_mobile =/^ (?: 13\d|15\d) \d{5} (\d{3}|\*{3}) $/; Phone format, need to change
var pattern_phone=/^ ((0\d{2,3})-)? (\d{7,8}) (-(\d{3,}))? $/;
var objvalue=$ (obj). val ();
if (!pattern_mobile.test (objvalue) | |!pattern_phone.test (objvalue)) {
Alert ("Please fill in the correct phone number, such as: 13755222222 or 0312-6666666");
}
}
</script> Transferred from: http://skybirdzw.blog.163.com/blog/static/725706262012482124880/

Common regular validation for jquery

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.