Complete JavaScript Form Verification

Source: Internet
Author: User

Function is_empty (VE ){
// Null value verification: null returns true; otherwise, false is returned.
If (VE. length> 0 ){
Return false;
}
Return true;
}
Function is_string (VE ){
// String: Valid. True is returned. Otherwise, false is returned.
VaR Re =/^ ([A-Z] * | [A-Z] * |/D * | [-_/~! @ #/$ %/^ &/*/./(/)/[/]/{/} <> /? //// '/"] *) |. {0, 5}) $ |/S/
If (Re. Test (VE )){
Return false;
}
Return true;
}
Function is_number (VE ){
// Number: True
VaR Re =/^/d + $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_integer (VE ){
// Positive integer: True
VaR Re =/^ [0-9] * [1-9] [0-9] * $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_double (VE ){
// Positive floating point: True
VaR Re =/^ [-/+]? /D + (/./d + )? $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_character (VE ){
// Is a character: True
VaR Re =/^ [A-Za-Z] + $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_chinese (VE ){
// Chinese Character: True
VaR Re =/^ [/u0391-/uffe5] + $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_email (VE ){
// Mail address: True
VaR Re =/^/W + ([-+.] /W +) * @/W + ([-.] /W + )*/. /W + ([-.] /W +) * $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_phone (VE ){
// Yesphone number: True
VaR Re =/^ (/d {3}/) | (/d {3 }/-))? (/(0/d {2, 3}/) | 0/d {2, 3 }-)? [1-9]/d {6, 7} $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_mobile (VE ){
// Mobile phone number: True
VaR Re =/^ (/d {3}/) | (/d {3 }/-))? 13/d {9} $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_idcard (VE ){
// ID number: True
VaR Re =/^/d {15} (/d {2} [A-Za-z0-9])? $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_url (VE ){
// URL: True
VaR Re =/^ http: // [A-Za-z0-9] +/. [A-Za-z0-9] + [// = /? % /-&_~ '@ [/]/': +!] * ([^ <>/"/"]) * $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_zip (VE ){
// Zip code: True
VaR Re =/^ [0-9]/d {5} $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_qq (VE ){
// QQ number: True
VaR Re =/^ [1-9]/d {4, 15} $/
If (! Re. Test (VE )){
Return false;
}
Return true;
}
Function is_tel (VE ){
// Call: True
VaR Re =/^ [()-] | [0-9] $/
If (! Re. Test (VE )){
Return false;
}
Return true;

}
Function is_date (VE, formatstring ){
// Date: True
Formatstring = formatstring | "ymd ";
VaR M, year, month, day;
Switch (formatstring ){
Case "ymd ":
M = ve. match (New Regexp ("^ (// d {4}) | (// d {2 }))([-. /]) (// d {1, 2}) // 4 (// d {1, 2}) $ "));
If (M = NULL) return false;
Day = m [6];
VaR M_n = Int = parseint (M [5]);
Month = m_n-1;
Year = (M [2]. Length = 4 )? M [2]: getfullyear (parseint (M [3], 10 ));
Break;
Case "DMY ":
M = op. match (New Regexp ("^ (// d {1, 2 })([-. /]) (// d {1, 2}) // 2 (// d {4}) | (// d {2}) $ "));
If (M = NULL) return false;
Day = m [1];
Month = m [3] --;
Year = (M [5]. Length = 4 )? M [5]: getfullyear (parseint (M [6], 10 ));
Break;
Default:
Break;
}
// If (! Parseint (month) return false;
// Month = 12? 0: month;
VaR date = new date (year, month, day );
Return (typeof (date) = "object" & year = date. getfullyear () & month = date. getmonth () & day = date. getdate ());
Function getfullyear (y) {return (Y <30? "20": "19") + Y) | 0 ;}
}
Function is_limitstr (VE, Vmin, Vmax ){
// Length limit
Vmin = Vmin | 0;
Vmax = vmax | number. max_value;
Return Vmin <= VE. Length & VE. Length <= vmax;
}
Function is_limitnum (VE, Vmin, Vmax ){
// Numerical limit
Vmin = Vmin | 0;
Vmax = vmax | number. max_value;
Ve = parseint (VE );
If (VE> = Vmin & Vmax> = VE ){
Return true;
}
Return false;
// Return Vmin <= ve & ve <= vmax;
}
Function is_submit (theform ){
// Submit the form theform: Return true, otherwise a prompt is displayed.
VaR erroritem = new array ();
VaR errormessage = new array ();
VaR errlength = 1;
VaR OBJ = theform | event. srcelement;
Errormessage. Length = 1;
Erroritem. Length = 1;
Errormessage [0] = "prompt:/T/N ";
Erroritem [0] = OBJ;
VaR COUNT = obj. elements. length;
For (VAR I = 0; I <count; I ++ ){
With (obj. elements [I]) {
VaR _ objtype = getattribute ("type ");
// If (typeof (_ datatype) = "object") continue;
If (getattribute ("type") = "hidden") continue;
VaR _ datatype = getattribute ("datatype ");
If (typeof (_ datatype) = "object") continue;
If (getattribute ("require") = "false" & value = "") continue;
Switch (_ datatype ){
Case "noempty ":
If (eval (is_empty (value ))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Case "Limit ":
If (eval (is_limitstr (value, getattribute ('Min'), getattribute ('max ')))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Case "Number ":
If (! Eval (is_number (value ))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Case "limitnum ":
If (! Eval (is_limitnum (value, getattribute ('Min'), getattribute ('max ')))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Case "double ":
If (! Eval (is_double (value ))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Case "email ":
If (! Eval (is_email (value ))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Case "mobile ":
If (! Eval (is_mobile (value ))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Case "phone ":
If (! Eval (is_phone (value ))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Case "tel ":
If (! Eval (is_tel (value ))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Case "QQ ":
If (! Eval (is_qq (value ))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Case "Zip ":
If (! Eval (is_zip (value ))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Case "date ":
If (! Eval (is_date (value, getattribute ('format ')))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
Break;
Default:
/* If (eval (is_string (value ))){
Erroritem [erroritem. Length] = erroritem [0]. elements [I]
Errormessage [errormessage. Length] = getattribute ("MSG ");
}
*/
Break;
}
}
}
If (errormessage. length> 1 ){
VaR errcount = erroritem. length;
For (VAR I = 1; I <errcount; I ++)
Erroritem [I]. style. color = "red ";
Alert (errormessage. Join ("/N "));
Erroritem [1]. Focus ();
Return false;
}

Return true;

}
/*************************************** ***********************
Common scripts
**************************************** ***********************/
Function setfocus (OB ){
// Set the cursor
Ob. Focus ();
}
Function keyenter (OB ){
// Move the cursor
Key = event. keycode;
If (Key = 13 ){
Ob. Focus ();
}
}
// Remove Spaces
String. Prototype. Trim = function ()
{
Return this. Replace (/(^/S *) | (/S * $)/g ,"");
}

Function movr (SRC, clrover ){
If (! SRC. Contains (event. fromelement )){
SRC. bgcolor = clrover;
}
}
Function mout (SRC, clrin ){
If (! SRC. Contains (event. toelement )){
SRC. style. cursor = 'default ';
SRC. bgcolor = clrin;
}
}

 

Function checkint (me ){
VaR S = "0123456789"
VaR c = string. fromcharcode (event. keycode)
If (S. indexof (c) <0 ){
Event. keycode = 0;
Return false;
}
}

Function checkfloat (me ){
VaR S = "0123456789 ."
VaR c = string. fromcharcode (event. keycode)
If (S. indexof (c) <0 ){
Event. keycode = 0;
Return false;
}
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.