JS Judgment code full collection _javascript skill

Source: Internet
Author: User
Tags lowercase
/*$#################################################$*/
/* Program function: Input Verification * *
/* Function Name: */
/* Function Checkdata (valname,val,valimode,limitlen) * *
/* Feature Description: Verify string Data * *
/* Function Checkusername (val,min,max) * *
/* Function Description: Verify user name */
/* Function Checkpassword (val,min,max) * *
/* Function Description: Verify password * *
/* Function Issame (val1,val2) * *
/* Verify password Match * *
/* Function Checkemail (val,mode) * *
/* Function Description: Verify Email * *
/* Function Checkvalheight (val,min,max,mode) * *
/* Function Description: Verify the user height * *
/* Function Checkvalweight (val,min,max,mode) * *
/* Function Description: Verify the user weight * *
/* Function CheckNumber (val,min,max,mode) * *
/* Function Description: Verify the value * *
/* Function checkpositive (val,min,max,mode) * *
/* Function Description: Verify positive value * *
/* Function checknegative (val,min,max,mode) * *
/* Function Description: Verify negative value * *
/* Function Checkfloat (val,min,max,mode) * *
/* Function Description: Verify the floating-point value * *
/* Function Checkpath (val,mode) * *
/* Function Description: Verify file path * *
/* Function Checkurl (val,mode) * *
/* Function Description: Verify URL * *
/* Function Checkchinaidcard_j (val,mode) * *
/* Function Description: Verify ID Card * *
/* function Isinteger (snum) * *
/* Function Description: Verify the integer * *
/* Function Checkzip (val,slen,mode) * *
/* Function Description: Verify ZIP * *
/* Input parameters: Mode True & False (whether allowed to empty) * *
/* Val form (judged), VA2 form (judged) * *
/* MAX (max), min (min) ... *
/* Write Time: 2003.09.08.2214 * *
/*$#################################################$*/
Validating string data
function Checkdata (Valname,val,valimode,limitlen) {
if (Val.value.indexOf ("<", 0) >0 | | val.value.indexOf (">", 0) >0) {
Alert (valname+) cannot contain < or > characters!. ");
Val.focus ();
Val.select ();
return false;
}
if (valimode==1) {
if (Isblank (Val.value)) {
Alert (valname+ "cannot be empty!");
Val.focus ();
Val.select ();
return false;
}
Tmp=new String (Val.value)
if (Istoolong (Tmp,limitlen)) {
Alert (valname+ "can not exceed the" +limitlen+ "character!");
Val.focus ();
Val.select ();
return false;
}
}
else{
Tmp=new String (Val.value)
if (Istoolong (Tmp,limitlen)) {
Alert (valname+ "cannot exceed" +limitlen+ "character!!");
Val.focus ();
Val.select ();
return false;
}
}
return true;
}
function Isblank (str) {
var i;
var len=str.length;
for (I=0;i<len;++i) {
if (Str.charat (i)!= "")
return false;
}
return true;
}
function Istoolong (str,i) {
var len=str.length;
if (i==0)
return false;
else{
if (len>i)
return true;
Else
return false;
}
}
Verifying user Names
function Checkusername (Val,min,max) {
var len = val.value.length;
if (Len<min | | len>max) {
Alert ("The user name is not the correct length, should be" +min+ "-" +max+ "English letter, number. ");
Val.focus ();
Val.select ();
return false;
}
for (i=0;i<val.value.length;i++) {
var ch = val.value.charAt (i);
if ((ch< "0" | | ch> "9") && (ch< "a" | | ch > "Z")) {
Alert ("User name must consist of lowercase letters or numbers.) ");
Val.focus ();
Val.select ();
return false;
}
}
var-=val.value.charat (0)
if (first< "a" | | | ch > "Z") {
Alert ("User name must begin with a lowercase letter.) ");
Val.focus ();
Val.select ();
return false;
}
return true;
}
Verify Password
function Checkpassword (Val,min,max) {
var len = val.value.length;
if (Len<min | | len>max) {
Alert ("Password length is incorrect, should be" +min+ "-" +max+ "letter, number. ");
Val.focus ();
Val.select ();
return false;
}
for (i=0;i<val.value.length;i++) {
var ch = val.value.charAt (i);
if ((ch< "0" | | ch> "9") && (ch< "a" | | ch > "Z")) {
Alert ("Password must consist of lowercase letters or numbers.) ");
Val.focus ();
Val.select ();
return false;
}
}
return true;
}
Verify password match
function Issame (val1,val2) {
if (Val1.value!= val2.value) {
Alert ("The password entered two times is not the same.) ");
Val2.focus ();
Val2.select ();
return false;
}
Else
return true;
}
Verify Email
function Checkemail (val,mode) {
if ((mode==0) && (val.value== "")) {
return true;
}
var etext
var Elen
var i
var AA
Etext=val.value
Elen=etext.length
if (elen<5) {
Alert ("Please enter the correct email.") ");
Val.focus ();
Val.select ();
return false;
}
i= etext.indexof ("@", 0)
if (i==0 | | i==-1 | | i==elen-1) {
Alert ("Please enter the correct email.") ");
Val.focus ();
Val.select ();
return false;
}
else{
if (Etext.indexof ("@", i+1)!=-1) {
Alert ("Please enter the correct email.") ");
Val.focus ();
Val.select ();
return false;
}
}
if (Etext.indexof ("..", i+1)!=-1) {
Alert ("Please enter the correct email.") ");
Val.focus ();
Val.select ();
return false;
}
I=etext.indexof (".", 0)
if (i==0 | | i==-1 | | | Etext.charat (elen-1) = = '. ') {
Alert ("Please enter the correct email.") ");
Val.focus ();
Val.select ();
return false;
}
if (Etext.charat (0) = = '-' | | Etext.charat (elen-1) = = ') {
Alert ("Please enter the correct email.") ");
Val.focus ();
Val.select ();
return false;
}
if (Etext.charat (0) = = ' _ ' | | Etext.charat (elen-1) = = ' _ ') {
Alert ("Please enter the correct email.") ");
Val.focus ();
Val.select ();
return false;
}
for (i=0;i<=elen-1;i++) {
Aa=etext.charat (i)
if (!) ( (aa== '. ') || (aa== ' @ ') | | (aa== '-') | | (aa== ' _ ') | | (aa>= ' 0 ' && aa<= ' 9 ') | | (aa>= ' A ' && aa<= ' z ') | | (aa>= ' A ' && aa<= ' Z ')) {
Alert ("Please enter the correct email.") ");
Val.focus ();
Val.select ();
return false;
}
}
return true;
}
Verify User Height
function Checkvalheight (val,min,max,mode) {
if ((mode==0) && (val.value== "")) {
return true;
}
var len = val.value.length;
if (Len<min | | len>max) {
Alert ("Please enter the correct height data.") ");
Val.focus ();
Val.select ();
return false;
}
var val2 = Val.value;
for (i=0;i<val2.length;i++) {
var ch = val2.charat (i);
if ((ch< "0" | | | ch> "9")) {
Alert ("Height data must consist of valid digits.") ");
Val.focus ();
Val.select ();
return false;
}
}
if (isNaN (Val2)) {
Alert ("Height data must consist of valid digits.") ");
Val.focus ();
Val.select ();
return false;
}
else if (parseint (val2) <=0) {
Alert ("Height data must consist of valid digits.") ");
Val.focus ();
Val.select ();
return false;
}
else if (Val2.indexof (' 0 ') ==0) {
Alert ("Height data must consist of valid digits.") ");
Val.focus ();
Val.select ();
return false;
}
return true;
}
Verifying user Weight
function Checkvalweight (val,min,max,mode) {
if ((mode==0) && (val.value== "")) {
return true;
}
var len = val.value.length;
if (Len<min | | len>max) {
Alert ("Please enter the correct weight data.") ");
Val.focus ();
Val.select ();
return false;
}
var val2 = Val.value;
for (i=0;i<val2.length;i++) {
var ch = val2.charat (i);
if ((ch< "0" | | | ch> "9")) {
Alert ("Weight data must consist of valid digits.") ");
Val.focus ();
Val.select ();
return false;
}
}
if (isNaN (Val2)) {
Alert ("Weight data must consist of valid digits.") ");
Val.focus ();
Val.select ();
return false;
}
else if (parseint (val2) <=0) {
Alert ("Weight data must consist of valid digits.") ");
Val.focus ();
Val.select ();
return false;
}
else if (Val2.indexof (' 0 ') ==0) {
Alert ("Weight data must consist of valid digits.") ");
Val.focus ();
Val.select ();
return false;
}
return true;
}
Validating values
function CheckNumber (val,min,max,mode) {
if ((mode==0) && (val.value== "")) {
return true;
}
var len = val.value.length;
if (Len<min | | len>max) {
Alert ("Please enter the correct value.") ");
Val.focus ();
Val.select ();
return false;
}
var val2 = Val.value;
for (i=1;i<val2.length;i++) {
var ch = val2.charat (i);
if ((ch< "0" | | | ch> "9")) {
Alert ("Please enter the correct value.") ");
Val.focus ();
Val.select ();
return false;
}
}
if (isNaN (Val2)) {
Alert ("Please enter the correct value.") ");
Val.focus ();
Val.select ();
return false;
}
else if (Val2.indexof (' 0 ') ==0 && len>1) {
Alert ("Please enter the correct valid value.") ");
Val.focus ();
Val.select ();
return false;
}
else if (val2.indexof ('-') ==0 && val2.indexof (' 0 ') ==1) {
Alert ("Please enter the correct valid value.") ");
Val.focus ();
Val.select ();
return false;
}
return true;
}
Validating positive values
function Checkpositive (val,min,max,mode) {
if ((mode==0) && (val.value== "")) {
return true;
}
if (! CheckNumber (Val,min,max,mode)) {
Val.focus ();
Val.select ();
return false;
}
if (parseint (val.value) <=0) {
Alert ("The data must be a valid positive integer.") ");
Val.focus ();
Val.select ();
return false;
}
return true;
}
Validating negative values
function Checknegative (val,min,max,mode) {
if ((mode==0) && (val.value== "")) {
return true;
}
if (! CheckNumber (Val,min,max,mode)) {
Val.focus ();
Val.select ();
return false;
}
if (parseint (val.value) >=0) {
Alert ("The data must be a valid negative integer.") ");
Val.focus ();
Val.select ();
return false;
}
return true;
}
Verifying floating-point values
function Checkfloat (val,min,max,mode) {
if ((mode==0) && (val.value== "")) {
return true;
}
var len = val.value.length;
if (Len<min | | len>max) {
Alert ("Please enter the correct floating-point number.") ");
Val.focus ();
Val.select ();
return false;
}
var val2 = Val.value;
for (i=1;i<val2.length;i++) {
var ch = val2.charat (i);
if ((ch< "0" | | | ch> "9")) {
if (ch!= ".") {
Alert ("Please enter the correct floating-point number.") ");
Val.focus ();
Val.select ();
return false;
}
}
}
if (isNaN (Val2)) {
Alert ("Please enter the correct floating-point number.") ");
Val.focus ();
Val.select ();
return false;
}
else if (Val2.indexof (' 0 ') ==0 && val2.indexof ('. ')! =1) {
Alert ("Please enter the correct valid floating-point value.") ");
Val.focus ();
Val.select ();
return false;
}
else if (val2.indexof ('-') ==0 && val2.indexof (' 0 ') ==1 && val2.indexof ('. ')! =2) {
Alert ("Please enter the correct valid floating-point value.") ");
Val.focus ();
Val.select ();
return false;
}
else if (val2.indexof ('-') ==0 && val2.indexof ('. ') ==1) {
Alert ("Please enter the correct valid floating-point value.") ");
Val.focus ();
Val.select ();
return false;
}
else if (Val2.indexof ('. ') ==0) {
Alert ("Please enter the correct valid floating-point value.") ");
Val.focus ();
Val.select ();
return false;
}
return true;
}
Verifying file paths
function Checkpath (val,mode) {
if ((mode==0) && (val.value== "")) {
return true;
}
var val2 = Val.value;
if (Val2.indexof (': \ \ ') >0) {
var isnot= "!@#$^* () ' ~|]} [{;. >,<?%&+= ";
if (Val2.indexof (' \ ") >0) {
Alert ("Please enter the correct file path.") ");
Val.focus ();
Val.select ();
return false;
}
else{
for (var i = 0;i < val2.length;i++) {
for (var x = 1; x < isnot.length;x++) {
if (Val2.charat (i) ==isnot.charat (x)) {
Alert ("Please enter the correct file path.") ");
Val.focus ();
Val.select ();
return false;
}
}
}
}
}
else{
Alert ("Please enter the correct file path.") ");
Val.focus ();
Val.select ();
return false;
}
return true;
}
Verifying URLs
function Checkurl (val,mode) {
if ((mode==0) && (val.value== "")) {
return true;
}
var val2 = Val.value;
if (Val2.indexof ('://') >0) {
var isnot= "!@$^* () ' ~|]} [{;. >,< ";
if (Val2.indexof (' \ ") >0) {
Alert ("Please enter the correct URL.") ");
Val.focus ();
Val.select ();
return false;
}
else{
for (var i = 0;i < val2.length;i++) {
for (var x = 1; x < isnot.length;x++) {
if (Val2.charat (i) ==isnot.charat (x)) {
Alert ("Please enter the correct URL.") ");
Val.focus ();
Val.select ();
return false;
}
}
}
}
}
else{
Alert ("Please enter the correct URL.") ");
Val.focus ();
Val.select ();
return false;
}
return true;
}
Verify ID
function Checkchinaidcard_j (val,mode) {
if ((mode==0) && (val.value== "")) {
return true;
}
SNo = val.value.toString ()
if (!isinteger (SNo)) {
Alert ("Please enter the correct ID.") ");
Val.focus ();
Val.select ();
return False
}
Switch (sno.length) {
Case 15:if (Isvaliddate (Sno.substr (6,2), Sno.substr (8,2), Sno.substr (10,2))) {
return True
}
Case 17:if (Isvaliddate (Sno.substr (6,4), Sno.substr (10,2), Sno.substr (12,2))) {
return True
}
}
Alert ("Please enter the correct ID.") ");
Val.focus ();
Val.select ();
return False
}
function Isinteger (snum) {
var num
Num=new RegExp (' [^0-9_] ', ')
if (isNaN (Snum)) {
return False
}
else {
if (Snum.search (num) >=0) {
return False
}
else {
return True
}
}
}
Verifying zip
function Checkzip (val,slen,mode) {
if ((mode==0) && (val.value== "")) {
return true;
}
if (Val.value.length!=slen) {
Alert (' Please enter the correct ZIP code!! ');
Val.focus ();
Val.select ();
return False
}
var R1
R1 = new RegExp (' [^0-9] ', ');
if (Val.value.search (R1) >= 0) {
Alert (' Please enter the correct ZIP code!! ');
Val.focus ();
Val.select ();
return False
}
Else
return true;
}
Verification of phone numbers
Requirements:
(1) The telephone number shall consist of numbers, "(") "and"-"
(2) Phone number is 3 to 8 digits
(3) If the phone number contains an area code, then the area code is three digits or four digits
(4) The area code is separated by "(", ")" or "-" and other parts
(5) Mobile phone number is 11 or 12 digits, if 12 digits, then the first digit is 0
(6) The first and second digits of the 11-digit mobile phone number are "13"
(7) The second and third digits of the 12-digit mobile phone number are "13"
According to these rules, you can go with the following regular expressions:
(^[0-9]{3,4}\-[0-9]{3,8}$) | (^[0-9]{3,8}$) | (^\ ([0-9]{3,4}\) [0-9]{3,8}$) | (^0{0,1}13[0-9]{9}$)
<script language= "JavaScript" >
function Phonecheck (s) {
var str=s;
var reg=/(^[0-9]{3,4}\-[0-9]{3,8}$) | (^[0-9]{3,8}$) | (^\ ([0-9]{3,4}\) [0-9]{3,8}$) | (^0{0,1}13[0-9]{9}$)/
Alert (Reg.test (str));
}
</script>
<input type=text name= "iphone" >
<input Type=button onclick= "Phonecheck (document.all.iphone.value)" value= "Check" >

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.