JavaScript code collection

Source: Internet
Author: User

/* $ ##################################### ############$ */
/* Program function: Enter verification */
/* Function name :*/
/* Function CheckData (valname, val, valimode, limitlen )*/
/* Function Description: Verify string data */
/* Function CheckUserName (val, min, max )*/
/* Function Description: Verify the user name */
/* Function CheckPassWord (val, min, max )*/
/* Function Description: Verify the 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: verification value */
/* Function CheckPositive (val, min, max, mode )*/
/* Function Description: verify positive value */
/* Function CheckNegative (val, min, max, mode )*/
/* Function Description: Verify the negative value */
/* Function CheckFloat (val, min, max, mode )*/
/* Function Description: Verify the floating point value */
/* Function CheckPath (val, mode )*/
/* Function Description: Verify the file path */
/* Function CheckURL (val, mode )*/
/* Function Description: Verify the URL */
/* Function CheckChinaIDCard_J (val, mode )*/
/* Function Description: Verify the ID card */
/* Function isInteger (sNum )*/
/* Function Description: Verify the integer */
/* Function CheckZip (val, slen, mode )*/
/* Function Description: Verify the Zip file */
/* Input parameter: true or false mode (whether to allow null )*/
/* Val form (items to be judged), va2 form (items to be judged )*/
/* Max (maximum), min (minimum )......*/
/* Compilation Time: 2003.09.08.2214 */
/* $ ##################################### ############$ */
// Verify 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 blank! ");
Val. focus ();
Val. select ();
Return false;
}
Tmp = new String (val. value)
If (istoolong (tmp, limitlen )){
Alert (valname + "cannot exceed" + limitlen + "characters! ");
Val. focus ();
Val. select ();
Return false;
}
}
Else {
Tmp = new String (val. value)
If (istoolong (tmp, limitlen )){
Alert (valname + "cannot exceed" + limitlen + "characters !! ");
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;
}
}
// Verify the user name
Function CheckUserName (val, min, max ){
Var len = val. value. length;
If (len <min | len> max ){
Alert ("the username length is incorrect. It should be" + min + "-" + max + "English letters and numbers. ");
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 ("the user name must consist of lower-case letters or numbers. ");
Val. focus ();
Val. select ();
Return false;
}
}
Var first = val. value. charAt (0)
If (first <"a" | ch> "z "){
Alert ("the user name must start with a lowercase letter. ");
Val. focus ();
Val. select ();
Return false;
}
Return true;
}
// Verify the password
Function CheckPassWord (val, min, max ){
Var len = val. value. length;
If (len <min | len> max ){
Alert ("the password length is incorrect. It should be" + min + "-" + max + "English letters and numbers. ");
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 lower-case 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 two passwords are different. ");
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 ("enter the correct Email. ");
Val. focus ();
Val. select ();
Return false;
}
I = etext. indexOf ("@", 0)
If (I = 0 | I =-1 | I = elen-1 ){
Alert ("enter the correct Email. ");
Val. focus ();
Val. select ();
Return false;
}
Else {
If (etext. indexOf ("@", I + 1 )! =-1 ){
Alert ("enter the correct Email. ");
Val. focus ();
Val. select ();
Return false;
}
}
If (etext. indexOf ("...", I + 1 )! =-1 ){
Alert ("enter the correct Email. ");
Val. focus ();
Val. select ();
Return false;
}
I = etext. indexOf (".", 0)
If (I = 0 | I =-1 | etext. charAt (elen-1) = '.'){
Alert ("enter the correct Email. ");
Val. focus ();
Val. select ();
Return false;
}
If (etext. charAt (0) = '-' | etext. charAt (elen-1) = '-'){
Alert ("enter the correct Email. ");
Val. focus ();
Val. select ();
Return false;
}
If (etext. charAt (0) = '_' | etext. charAt (elen-1) = '_'){
Alert ("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 ("enter the correct Email. ");
Val. focus ();
Val. select ();
Return false;
}
}
Return true;
}
// Verify the 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 ("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 numbers. ");
Val. focus ();
Val. select ();
Return false;
}
}
If (isNaN (val2 )){
Alert ("height data must consist of valid numbers. ");
Val. focus ();
Val. select ();
Return false;
}
Else if (parseInt (val2) <= 0 ){
Alert ("height data must consist of valid numbers. ");
Val. focus ();
Val. select ();
Return false;
}
Else if (val2.indexOf ('0') = 0 ){
Alert ("height data must consist of valid numbers. ");
Val. focus ();
Val. select ();
Return false;
}
Return true;
}
// Verify the 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 ("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 numbers. ");
Val. focus ();
Val. select ();
Return false;
}
}
If (isNaN (val2 )){
Alert ("weight data must consist of valid numbers. ");
Val. focus ();
Val. select ();
Return false;
}
Else if (parseInt (val2) <= 0 ){
Alert ("weight data must consist of valid numbers. ");
Val. focus ();
Val. select ();
Return false;
}
Else if (val2.indexOf ('0') = 0 ){
Alert ("weight data must consist of valid numbers. ");
Val. focus ();
Val. select ();
Return false;
}
Return true;
}
// Verify the value
Function CheckNumber (val, min, max, mode ){
If (mode = 0) & (val. value = "")){
Return true;
}
Var len = val. value. length;
If (len <min | len> max ){
Alert ("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 ("enter the correct value. ");
Val. focus ();
Val. select ();
Return false;
}
}
If (isNaN (val2 )){
Alert ("enter the correct value. ");
Val. focus ();
Val. select ();
Return false;
}
Else if (val2.indexOf ('0') = 0 & len> 1 ){
Alert ("enter the correct valid value. ");
Val. focus ();
Val. select ();
Return false;
}
Else if (val2.indexOf ('-') = 0 & val2.indexOf ('0') = 1 ){
Alert ("enter the correct valid value. ");
Val. focus ();
Val. select ();
Return false;
}
Return true;
}
// Verify the positive value
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 ("data must be a valid positive integer. ");
Val. focus ();
Val. select ();
Return false;
}
Return true;
}
// Verify the negative value
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 ("data must be a valid negative integer. ");
Val. focus ();
Val. select ();
Return false;
}
Return true;
}
// Verify the floating point value
Function CheckFloat (val, min, max, mode ){
If (mode = 0) & (val. value = "")){
Return true;
}
Var len = val. value. length;
If (len <min | len> max ){
Alert ("enter the correct floating point 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 ")){
If (ch! = "."){
Alert ("enter the correct floating point value. ");
Val. focus ();
Val. select ();
Return false;
}
}
}
If (isNaN (val2 )){
Alert ("enter the correct floating point value. ");
Val. focus ();
Val. select ();
Return false;
}
Else if (val2.indexOf ('0') = 0 & val2.indexOf ('.')! = 1 ){
Alert ("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 ("enter the correct valid floating point value. ");
Val. focus ();
Val. select ();
Return false;
}
Else if (val2.indexOf ('-') = 0 & val2.indexOf ('.') = 1 ){
Alert ("enter the correct valid floating point value. ");
Val. focus ();
Val. select ();
Return false;
}
Else if (val2.indexOf ('.') = 0 ){
Alert ("enter the correct valid floating point value. ");
Val. focus ();
Val. select ();
Return false;
}
Return true;
}
// Verify the file path
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 ("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 ("enter the correct file path. ");
Val. focus ();
Val. select ();
Return false;
}
}
}
}
}
Else {
Alert ("enter the correct file path. ");
Val. focus ();
Val. select ();
Return false;
}
Return true;
}
// Verify the URL
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 ("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 ("enter the correct URL. ");
Val. focus ();
Val. select ();
Return false;
}
}
}
}
}
Else {
Alert ("enter the correct URL. ");
Val. focus ();
Val. select ();
Return false;
}
Return true;
}
// Verify the ID card
Function CheckChinaIDCard_J (val, mode ){
If (mode = 0) & (val. value = "")){
Return true;
}
SNo = val. value. toString ()
If (! IsInteger (sNo )){
Alert ("enter the correct ID card. ");
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 ("enter the correct ID card. ");
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
}
}
}
// Verify the Zip code
Function CheckZip (val, slen, mode ){
If (mode = 0) & (val. value = "")){
Return true;
}
If (val. value. length! = Slen ){
Alert ('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 ('Enter the correct zip code !! ');
Val. focus ();
Val. select ();
Return false
}
Else
Return true;
}
Phone number verification
Requirements:
(1) Telephone Numbers consist of numbers, "(", ")", and "-"
(2) 3 to 8 phone numbers
(3) If the telephone number contains a district code, the district code is three or four digits.
(4) area codes are separated by "(", ")" or "-" and other sections.
(5) the mobile phone number is 11 or 12 digits. If the mobile phone number is 12 digits, the first digit is 0.
(6) The first and second digits of 11 mobile phone numbers are "13"
(7) The second and third digits of the 12-digit mobile phone numbers are "13"
Based on these rules, you can use 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.