Form Verification JS

Source: Internet
Author: User

// File: Check. js

// Check whether it is composed of digits
Function isdigit (s)
{
VaR patrn =/^ [0-9] {1, 20} $ /;
If (! Patrn.exe C (s) return false
Return true
}

// Check Logon Name: You can enter only 5-20 strings starting with a letter, which can contain numbers, "_", and ".".
Function isregisterusername (s)
{
VaR patrn =/^ [A-Za-Z] {1} ([a-zA-Z0-9] | [. _]) {} $ /;
If (! Patrn.exe C (s) return false
Return true
}

// Check user name: only 1-30 strings starting with letters can be entered
Function istruename (s)
{
VaR patrn =/^ [A-Za-Z] {1, 30} $ /;
If (! Patrn.exe C (s) return false
Return true
}

// Password verification: only 6-20 letters, numbers, and underscores can be entered
Function ispasswd (s)
{
VaR patrn =/^ (\ W) {6, 20} $ /;
If (! Patrn.exe C (s) return false
Return true
}

// Verify the phone number and fax number. The phone number can start with "+" and contain "-" in addition to numbers.
Function istel (s)
{
// Var patrn =/^ [+] {0, 1} (\ D) {1, 3} []? ([-]? (\ D) {1, 12}) + $ /;
VaR patrn =/^ [+] {0, 1} (\ D) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /;
If (! Patrn.exe C (s) return false
Return true
}

// Verify the mobile phone number. It must start with a number and can contain "-" in addition to a number.
Function ismobil (s)
{
VaR patrn =/^ [+] {0, 1} (\ D) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /;
If (! Patrn.exe C (s) return false
Return true
}

// Verify the zip code
Function ispostalcode (s)
{
// Var patrn =/^ [a-zA-Z0-9] {3, 12} $ /;
VaR patrn =/^ [a-zA-Z0-9] {} $ /;
If (! Patrn.exe C (s) return false
Return true
}

// Email Detection
Function isemail (thestr ){

VaR atindex = thestr. indexof ('@');
VaR dotindex = thestr. indexof ('.', atindex );
VaR flag = true;
Thesub = thestr. substring (0, dotindex + 1)

If (atindex <1) | (atindex! = Thestr. lastindexof ('@') | (dotindex <atindex + 2) | (thestr. Length <= thesub. Length ))
{Return (false );}
Else {return (true );}
}
// Judge whether it is null
Function isempty (STR ){
If (STR = NULL) | (Str. Length = 0) return true;
Else return (false );
}

// Determine the date
Function isdate (thestr ){
VaR the1st = thestr. indexof ('-');
VaR the2nd = thestr. lastindexof ('-');

If (the1st = the2nd ){
Return false;
} Else {
Var y = thestr. substring (0, the1st );
VaR M = thestr. substring (the1st + 1, the2nd );
VaR d = thestr. substring (the2nd + 1, thestr. Length );
VaR maxdays = 31;

If (isint (m) = false | isint (d) = false | isint (y) = false ){
Return false;
} Else if (Y. Length <4 ){
Return false;
} Else if (! Isbetween (M, 1, 12 )){
Return false;
} Else if (M = 4 | M = 6 | M = 9 | M = 11 ){
Maxdays = 30;
} Else if (M = 2 ){
If (Y % 4> 0 ){
Maxdays = 28;
} Else if (Y % 100 = 0 & Y % 400> 0 ){
Maxdays = 28;
} Else {
Maxdays = 29;
}
}
If (isbetween (d, 1, maxdays) = false ){
Return false;
}
Else {return true ;}
}
}

// Judgment range
Function isbetween (Val, lo, hi ){
If (Val <LO) | (Val> Hi) {return (false );}
Else {return (true );}
}

// Judge whether it is int type
Function isint (thestr ){
VaR flag = true;

If (isempty (thestr) {flag = false ;}
Else
{For (VAR I = 0; I <thestr. length; I ++ ){
If (isdigit (thestr. substring (I, I + 1) = false ){
Flag = false; break;
}
}
}
Return (FLAG );
}

// Common Verification Method for form elements
// Whether the Nu detection is null
// Whether the nm detection is Numeric
// Check whether the EM is mail
// Le = 12 detection Length
// YY year
// Mm Month
// Dd Day
Function validateform (ADD ){
VaR alertmessage = "";
VaR elarr = eval (document. Forms [0]. Name). elements;
VaR stryy = "";
VaR strmm = "";
VaR strdd = "";
For (VAR I = 0; I <elarr. length; I ++ ){
If (elarr [I]. ID ){
VaR arylist = (elarr [I]. ID). Split (',');
For (var j = 0; j <arylist. length; j ++ ){
If (arylist [J]. substring (0, 2) = "Nu "){
If (elarr [I]. value = ""){
Alertmessage = alertmessage + elarr [I]. Title + "cannot be blank! \ N ";
Break;
}
}
If (arylist [J]. substring (0, 2) = "Nm "){
If (elarr [I]. value! = ""){
If (isdigit (elarr [I]. Value) = false ){
Alertmessage = alertmessage + elarr [I]. Title + "not a number! \ N ";
Break;
}
}
}
If (arylist [J]. substring (0, 2) = "em "){
If (elarr [I]. value! = ""){
If (isemail (elarr [I]. Value) = false ){

Alertmessage = alertmessage + elarr [I]. Title + "Incorrect format! \ N ";
Break;
}
}
}
If (arylist [J]. substring (0, 2) = "Le "){
If (elarr [I]. value! = ""){
VaR elelength = elarr [I]. value. length;
VaR ordlength = arylist [J]. substring (3,100 );
If (elelength> ordlength ){
Alertmessage = alertmessage + elarr [I]. Title + "length requirement:" + ordlength + "bit! \ N ";
Break;
}
}
}
If (arylist [J]. substring (0, 2) = "YY "){
Stryy = elarr [I]. value;
}
If (arylist [J]. substring (0, 2) = "mm "){
Strmm = elarr [I]. value;
}
If (arylist [J]. substring (0, 2) = "DD "){
Strdd = elarr [I]. value;
}
If (stryy! = "" & Strmm! = "" & Strdd! = ""){
If (stryy! = "" & Strmm! = "" & Strdd! = ""){
VaR strdate = stryy + "-" + strmm + "-" + strdd;
If (isdate (strdate) = false ){
Alertmessage = alertmessage + "incorrect date format \ n ";
Stryy = "";
Strmm = "";
Strdd = "";
}
}
}

}
}
}

If (add = "true "){
Alertmessage = addmessage (alertmessage );
}

If (alertmessage! = ""){
Alert (alertmessage );
Return false;
} Else {
Return true;
}
}

Function checkboxchecked (checkboxname)
{
VaR COUNT = 0;
VaR formname = Document. Forms [0]. Name;
If (eval ("document." + formname + "." + checkboxname). length! = NULL)
{

For (VAR I = 0; I <eval ("document." + formname + "." + checkboxname). length; I ++)
{
If (eval ("document." + formname + "." + checkboxname) [I]. Checked = true)
{

Count = count + 1;
}
}
}
Else
{
If (eval ("document." + formname + "." + checkboxname). Checked = true)
{
Count = count + 1;
}
}
Return count;
}

// File: index.html

<HTML>
<Script language = "JavaScript" src = "check. js"> </SCRIPT>
<SCRIPT>
Function addmessage (Message ){
Message = message + "added error message \ n"
Return message;
}
</SCRIPT>
<Body>
<Form name = "ordform" id = "ordform" method = "Post">
<Input type = "test" name = "name" id = "Nu, Nm" Title = "name">
<Input type = "test" name = "password" id = "Nu, Le = 12" Title = "password">
<Input type = "test" name = "mail" id = "Nu, em" Title = "email">
<Input type = "test" name = "yybirthday" id = "YY" Title = "name">
<Input type = "test" name = "mmbirthday" id = "mm" Title = "password">
<Input type = "test" name = "ddbirthday" id = "DD" Title = "email">
<Input type = "button" value = "Submit" onclick = "validateform (ordform)">
</Form>
</Body>
</Html>

// common Verification Method for form elements
// nu detection
// nm detection: Numeric
// em Detection: Mail
// Le = 12 detection length
// YY year
// mm Month
/dd Day

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.