jsp page based on $ .get () the only form validation field

// 1, validates the username
function CheckUserName (T) {
// Username: 6-12, non-empty, the first letter of
the let t.next span = ();
span.empty ();
the let V = t.val ( );
the let parent = t.parent ();
// a regular expression defines
the let REG = / ^ [a-zA-the Z] \ {W} $ 5,11 /;
IF (V) {!
span.html ( "user name can not be empty");
parent.removeClass ( "OK"). addClass ( "error");.
return 0;
} the else IF (reg.test (V!)) {
span.html ( "first letter of the user name , 6-12 ");
parent.removeClass (" OK "). addClass (." error ");
return 0;
} {the else
the let In Flag = 0;
$ = .ajaxSettings.async to false;
$ .get (" CheckUserName. JSP ", {the userName: V}, function (RES) {
IF (RES ===" null ") {
span.html (" username is available ");
parent.removeClass("error").addClass("ok");
flag = 1;
} else {
span.html("用户名被占用");
parent.removeClass("ok").addClass("error");
}
});
$.ajaxSettings.async = true;
return flag;
}

}

// 2, the password verification
function CheckPassword (T) {
// Password: _ 6-12 digit letters, non-empty
the let t.next span = ();
span.empty ();
the let V = t.val ();
parent = t.parent the let ();
// a regular expression defines
the let REG = / ^ \ {W} $ 6,12 /;
IF (V!) {
span.html ( "empty entry is not");
parent .removeClass ( "OK"). addClass ( "error");.
return 0;
} {the else IF (reg.test (V)!)
span.html ( "_ letter password 6-12 digits");
parent.removeClass ( "OK"). addClass ( "error");.
return 0;
}
span.html ( "password incorrect");
parent.removeClass ( "error"). addClass ( "OK");.
return. 1;
}

// 3, repeated authentication password
function checkRePassWord (T) {
// Password: _ 6-12 digit letters, non-empty
the let t.next span = ();
span.empty ();
the let V = t.val ( );
the let parent = t.parent ();
! IF (checkPassword ($ ( "# the userPassword"))) {
span.html ( "your password does not meet the requirements");
. parent.removeClass ( "the ok") addClass ( "error");
return 0;
}
IF (.! $ ( "# the userPassword") Val () == V) {
span.html ( "enter the password twice inconsistent");
parent.removeClass ( "OK") .addClass ( "error");
return 0;
}
span.html ( "re-enter the correct password");
parent.removeClass ( "error"). addClass ( "OK");.
return. 1;
}

// 4 verify that the phone number
function checkTel (T) {
// phone number: the number of non-empty, 11, the first bit is 1, the second 356 789
the let t.next span = ();
span.empty () ;
the let V = t.val ();
the let parent = t.parent ();
// a regular expression defines
the let REG = / ^. 1 [356 789] \ {D}. 9 $ /;
IF (! V) {
span .html ( "phone number can not be empty");
parent.removeClass ( "OK"). addClass ( "error");.
return 0;
(! reg.test (V))} {IF the else
span.html ( "phone 11-digit number, the first bit is 1, the second 356 789 ");
parent.removeClass (" OK "). addClass (." error ");
return 0;
} {the else
the let In Flag = 0;
$ .ajaxSettings.async to false =;
$ .get ( "checkPhone.jsp", {Phone: V}, function (RES) {
IF (RES === "null") {
span.html ( "Phone number is available ");
parent.removeClass("error").addClass("ok");
flag = 1;
} else {
span.html("电话号码被占用");
parent.removeClass("ok").addClass("error");
}
});
$.ajaxSettings.async = true;
return flag;
}
}

// 5, the verification mail
function The checkEmail (T) {
// email: non-empty, the reference @ 126.com Han, Han yahoo.com.cn @, @ Han yahoo.cn
the let t.next span = ();
span. empty ();
the let v = t.val ();
// define a regular expression
let reg = /^([a-zA-Z]\w+@\w+\\.com(\\.cn))? | ([A-zA-the Z] \ W + @ \ \\ W + CN.) $ /;
IF (! V) {
span.html ( "E-mail can not be empty") .css ( "color", "red") ;
return 0;
(! reg.test (V))} {IF the else
span.html ( "reference han @ 126.com, han @ yahoo.com.cn, han @ yahoo.cn") css ( "color". , "Red");
return 0;
}
span.html ( "correct mailbox") .css ( "Color", "Green");
return. 1;
}

// 6, gender verification
function checkGender (T) {
the let span = $ ( "gender_span.");
Span.empty ();
the let = In Flag to false;
for (the let I = 0; I <t.length; I ++) {
IF (T [I] .checked) {
In Flag to true =;
BREAK;
}
}
IF (In Flag!) {
span.html ( "must select a gender") .css ( "Color", "Red");
return 0;
}
span.html ( "select correct gender") .css ( "Color", "Green");
return. 1;
}

// 7, hobbies verification
function checkHobby (T) {
the let span = $ ( "hobby_span.");
Span.empty ();
the let = In Flag to false;
for (the let I = 0; I <t.length; I ++) {
IF (T [I] .checked) {
In Flag to true =;
BREAK;
}
}
IF (In Flag!) {
span.html ( "must select a hobby") .css ( "Color", "Red");
return 0;
}
span.html ( "select the correct hobby") .css ( "Color", "Green");
return 1;
}

// 8, ID verification
function checkIdentityNum (T) {
the let t.next span = ();
span.empty ();
the let V = t.val ();
IF (! V) {
span.html ( "ID can not be empty ") .css (" Color "," Red ");
return 0;
}
// determine the length
the let length = v.length;
! IF (length == 18) {
span.html (" ID 18 must bit ") .css (" Color "," Red ");
return 0;
}
// whether the first 17 bits of digital
the let pre17 v.substring = (0, v.length -. 1);
the let REG = / ^ \ D } $. 17 {/;
(! reg.test (pre17)) {IF
span.html ( "17 must be pre ID number") .css ( "Color", "Red");
return 0;
}
// 2, taken before determining whether the
the let pre2Arr = [
". 11", "12 is", "13 is", "14", "21", "22", "23",
"21", "22", "23",
"31", "32", "33", "34", "35", "36", "37",
"41", "42", "43", "44", "45", "46",
"50", "51", "52", "53", "54",
"61", "62", "63", "64", "65",
"81", "82", "83"
];
let pre2 = v.substring(0, 2);
let flag = false;
for (let i = 0; i < pre2Arr.length; i++) {
if (pre2Arr[i] === pre2) {
flag = true;
break;
}
}
if (!flag) {
span.html("身份证前2位不符合规则!").css("color", "red");
return 0;. span.html ( "ID year must be in 1900-2019") css ( "color"IF (year> the Date.getFullYear () || year <1900) {the let new new DATE = a Date ();the let year = the parseInt (v.substring (. 6, 10));// 5, taken year is determined to meet the requirements [1900-2019]
}





0 return;
}
//. 6, taken month meets the requirements [1-12]
the let the parseInt = month (v.substring (10, 12 is));
IF (month> 12 is || month <. 1) {
span.html ( " ID month must be 1-12. ") CSS (" Color "," Red ");
return 0;
}
//. 7, taken date meets the requirements [1-28] [1-29] [1-30] [1-31]
the let Day = the parseInt (v.substring (12 is, 14));
IF (month The month The === ===. 6. 4 || || ||. 9 month The month The === ===. 11) {
IF (Day> 30 Day || <= 0) {
span.html ( "date must be in the range ID 1-30") .css ( "Color", "Red");
return 0;
}
} the else IF (month the 2 ===) {
IF ((=== year. 4 0% 100% year &&! == 0) || year 400% === 0) {
IF (Day> 29 Day || <= 0) {
span .html ( "identity Card must be in the 1-29 range") .css ( "color", "red");
return 0;
}
} else {
IF (Day> 28 Day || <= 0) {
span.html ( "date must be in the 1-28 range ID") .css ( "Color", "Red");
return 0;
}
}
} {the else
IF (Day> Day 31 is || <= 0) {
span.html ( "date must be in the 1-31 range ID") .css ( "Color", "Red");
return 0;
}
}
//. 8, taken checksum
the let lastChar v.substring = (. 17);
REG = / ^ [\ DXX] $ /;
(! reg.test (lastChar)) {IF
span.html ( "ID check code only the last one could be digital, X-, X Color "," Red ") CSS (." ");
return 0;
}
span.html (" right ID ") .css (" Color "," Green ");
return. 1;
}

//9、验证上传文件类型及大小
function checkFiles(t) {
let arr = ["png", "jpg", "bmp", "jpeg", "gif"];
let span = t.next();
span.empty();
let name = "";
let size = 0;
let parent = t.parent();
//console.log(t);
if (t[0].files.length === 0) {
span.html("请上传文件");
parent.removeClass("ok").addClass("error");
return 0;
}
name = t[0].files[0].name;
size = t[0].files[0].size;
let suffix = name.substring(name.lastIndexOf(".") + 1);
let flag = false;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === suffix) {
flag = true;
break;
}
}
if (!flag) {
span.html ( "Please upload image file types");
parent.removeClass ( "OK"). addClass ( "error");.
return 0;
}
IF (size> 1024 * 1024 *. 3) {
span.html ( "Please Upload image size should not exceed 3MB ");
parent.removeClass (" the ok ") addClass (." error ");
return 0;
}
span.html (" picture right ");
parent.removeClass (." error ") addClass ( "the ok");
return 1;

}

// 10, the user authentication code, to verify the unique
function checkUserCode (T) {
// User code: 6-12, non-empty, the first letter of
the let t.next span = ();
span.empty ();
the let parent = t.parent ();
the let V = t.val ();
// a regular expression defines
the let REG = / ^ [a-zA-the Z] \ {W} $ 5,11 /;
IF (! V) {
span.html ( "user name can not be empty");
parent.removeClass ( "OK"). addClass ( "error");.
return 0;
} the else IF (! reg.test (V)) {
span.html ( " the first letter of the user name, 6-12 ");
parent.removeClass (" OK "). addClass (." error ");
return 0;
} the else {
// ajax request initiated occupied determines whether the user name
@ disposed ajax synchronization
let = 0 In Flag;
$ = .ajaxSettings.async to false;
$ .get ( "checkUserCode.jsp", {
userCode: V
},function (res) {
//console.log(res);
IF (RES === 'null') {
span.html ( "user code available");
parent.removeClass ( "error"). addClass ( "OK");.
In Flag =. 1;
} the else {
span.html ( " user code occupied ");
parent.removeClass (" OK "). addClass (." error ");
}
});
$ = .ajaxSettings.async to true;
return In Flag;
}
}

// 11, birthday verification
function checkBirthday (T) {
the let t.next span = ();
span.empty ();
the let parent = t.parent ();
the let V = t.val ();
IF (V!) {
span.html ( "not empty birthday!");
parent.removeClass ( "OK"). addClass ( "error");.
return 0;
}
span.html ( "correct birthday!");
parent.removeClass ( " error "). addClass (." OK ");
return. 1;
}


$ (function () {
//. 1, username loses focus verification
$ ( "# the userName") Blur (function () {.
Alert (CheckUserName ($ (the this)));
});

// 2, password authentication lost focus
$ ( "# the userPassword") Blur (function () {.
CheckPassword ($ (the this));
});

// 3, password authentication lost focus
$ ( "# userRemi") Blur. (Function () {
checkRePassWord ($ (the this));
});

// 4, tel loses focus verification
$ ( "Phone #") Blur (function () {.
CheckTel ($ (the this));
});

// 5, email authentication lost focus
$ ( "# In Email") Blur (function () {.
The checkEmail ($ (the this));
});

// 6, gender change
$ (. "Gender") Change (function () {.
CheckGender ($ ( "gender."));
});

// 7, hobby change
$ (. "Hobby") Change (function () {.
CheckHobby ($ ( "hobby."));
});

// 8, identityNum lost focus verification
$ ( "# identityNum") Blur. (Function () {
checkIdentityNum ($ (the this));
});

// 9, verify picpath change occurs, pay attention to the change event, otherwise no effect
$ ( "# picpath") change (function () {.
//Console.log($(this));
checkFiles ($ (the this) );
});

// 10, userCode lost focus verification
$ ( "# userCode") Blur (function () {.
CheckUserCode ($ (the this));
});

// 11, userCode lost focus verification
$ ( "# Birthday") Blur. (Function () {
checkBirthday ($ (the this));
});


// perform validation form submission, returns true if submitted, or do not submit
$ ( "# fm"). The Submit (function () {
the let NUM = CheckUserName ($ ( "# userName")) &
checkPassword ($ ( "# the userPassword ")) &
checkRePassWord ($ (" # userRemi ")) &
checkTel ($ (" # Phone ")) &
checkUserCode ($ (" # userCode ")) &
checkBirthday ($ (" # Birthday ")) &
checkFiles ($ ( "# picpath"));
return NUM> 0;
});
});

Guess you like

Origin www.cnblogs.com/lpzpp/p/11502876.html