Js Form Verification Method (practical) _ form special effects

Source: Internet
Author: User
Every time we use form verification, we are looking for something everywhere, and many of them are useless on the Internet. I finally accumulated these methods. Some of the methods used in my program are certainly easy to use, mainly using simple regular expressions for judgment. If you have any bugs, please submit them. // The verification below is the length
Function checkTextLen (textId ){
Var len = 0;
Var checkField = document. getElementById (textId );
Var inputstring = checkField. value;
Var string_length = inputstring. length;
If (string_length = 0)
{
Return 0;
}
For (var I = 0; I {
If (inputstring. charAt (I). charCodeAt ()> 255) len + = 2;
Else len + = 1;
}
Return len;
}
Function checkTextLength (textId, length, msg ){
Var textObj = document. getElementById (textId );
If (checkTextLen (textId)> length/1 ){
Alert ("[" + msg + "]" + "maximum length" + length + "bit," + "Please input again! Note: One Chinese Character occupies 2 places ");
TextObj. focus ();
Return false;
} Else {
Return true;
}
}
// The verification below does not contain invalid characters, including Chinese characters, English letters, and numbers.
Function isValidString (textId, errMsg ){
SzStr = document. getElementById (textId). value;
VoidChar = "'\"> <'~! @ # $ % ^ &\(\)()! ¥ ......?? "" '* ";
For (I = 0; I <voidChar. length; I ++ ){
AChar = voidChar. substring (I, I + 1 );
If (szStr. indexOf (aChar)>-1 ){
Alert (errMsg );
Return false;
}
}
Return true;
}
// Only letters, numbers, and underscores can be entered for verification below
Function isEnglish (textId, errMsg)
{
S = document. getElementById (textId). value;
// The length of the following regular expression is between 6 and 20
// Var patrn =/^ (\ w) {6, 20} $ /;
Var patrn =/^ (\ w) * $ /;
If (! Patrn.exe c (s )){
Alert (errMsg );
Return false
}
Return true
}
// The following verification only allows Chinese Characters
Function isChinese (textId, errMsg)
{
S = document. getElementById (textId). value;
Var patrn =/[^ \ u4E00-\ u9FA5]/g;
If (patrn.exe c (s )){
Alert (errMsg );
Return false
}
Return true
}
// The following verification only allows numbers
Function isNumber (textId, errMsg)
{
S = document. getElementById (textId). value;
// The length of the following regular expression is between 6 and 20
// Var patrn =/^ (\ d) {6, 20} $ /;
Var patrn =/^ (\ d) * $ /;
If (! Patrn.exe c (s )){
Alert (errMsg );
Return false
}
Return true
}
The Regular Expression in Javascript is used to control whether non-numbers can be entered in the text box, that is, only numbers can be entered. Call method: onkeyup = "onlyNum (this );"
Function onlyNum (obj)
{
Temp = obj. value;
// Note the regular expression below, which is not enclosed by quotation marks ..
Obj. value = temp. replace (/\ D/g ,'');
}
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.