Client form Generic Validation checkform (RPM)

Source: Internet
Author: User
Tags control characters
/**/ /*  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Client Form Universal Authentication Checkform (Oform)
*
* Function: Universal validation of all form elements.
*
Use
* <script language= "JavaScript" src= "Check.js" type= "Text/javascript" ></script>
* <form name= "Form1" onsubmit= "return Checkform (This)" >
* <input type= "name=" id "check=" ^/s+$ "warning=" ID cannot be empty and cannot contain spaces ">
* <input type= "Submit" >
* </form>
*
* Verify the legality of the Text,password,hidden,file,textarea,select,radio,checkbox
*
* Example of regular expressions:
* ^/s+$ cannot be empty and cannot contain spaces
*/s{6, 6-digit length
* ^/d+$ Digital
* ^/d+ (/./d+)? $ number, allow decimal point
* (. *) (/.jpg|/.bmp) $ file format jpg and BMP
* ^/d{4}/-/d{1,2}-/d{1,2}$
* ^ (19|20)/d/d/-(0|1)/d/-(0|1|2|3)/d$
* Date Format 2005-5-8
* ^0$ must choose one
* ^0{2,}$ Select two or more
* ^[/s|/s]{20,}$ can not be empty, and not less than 20 words
* ^[^/d/-_][/w/-]*[^/-_]$ User name format
* ^[/u4e00-/u9fa5]*$ Chinese characters
* ^[^/d/-_][/w/-]*[^/-_]@[^/-][a-za-z/d/-]*[^/-] (/.[ ^/-][a-za-z/d/-]*[^/-]) * *. [A-za-z] {3} (/. [A-za-z] {2})? $
* Email
*
* The contents of the regular expression:
* Character description
*/marks the next character as a special character, text, reverse reference, or octal escape character. For example, "n" matches the character "n". "/n" matches line breaks. Sequence '//' match '/', '/(' Match ' (".
* ^ matches the position where the input string begins. If the Multiline property of the RegExp object is set, ^ also matches the position after "/n" or "/R".
* $ matches the position of the end of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before "/n" or "/R".
* * 0 or more times to match the preceding character or subexpression. For example, zo* matches "z" and "Zoo". * is equivalent to {0,}.
* + matches the preceding character or subexpression one or more times. For example, "zo+" matches "Zo" and "Zoo", but does not match "Z". + is equivalent to {1,}.
*   ? Matches the preceding character or subexpression 0 times or once. For example, "Do (es)?" Match "Do" in "do" or "does". is equivalent to {0,1}.
* {n} n is a nonnegative integer. Matches n times exactly. For example, "o{2}" does not match "O" in "Bob", but matches two "o" in "food".
* {n,} n is a nonnegative integer. Match at least n times. For example, "o{2,}" does not match "O" in "Bob" and matches all o in "Foooood". ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.
* {n,m} m and n are nonnegative integers, where n <= m. Match at least n times, at most match m times. For example, "o{1,3}" matches the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' o '. Note: You cannot insert a space between commas and numbers.
*   ? When this character follows any other qualifier (*, + 、?、 {n}, {n,}, {n,m}), the matching pattern is "not greedy." The "not greedy" pattern matches the shortest possible string of searches, while the default "greedy" pattern matches the search for the longest string possible. For example, in the string "Oooo", "o+?" Matches only a single "O", and "o+" matches All "O".
*   . Matches any single character except "/n". To match any character including "/n", use a pattern such as "[/S/S]".
* (pattern) matches the pattern and captures the matching subexpression. You can use the $0$9 property to retrieve a captured match from the result "match" collection. To match the bracket character (), use "/(" or "/)".
* (?:p Attern) A subexpression that matches the pattern but does not capture the match, that is, it is a non capture match and does not store a match for later use. This is useful for using the "or" character (|) to combine a pattern part. For example, "Industr (?: y| ies)" is a more economical expression than "industry|industries".
* (? =pattern) performs a subexpression of a forward lookahead search that matches the string at the starting point of the string that matches the pattern. It is a non capture match, that is, a match cannot be captured for later use. For example, the Windows (? =95| 98| Nt| 2000) "Matches windows in Windows 2000, but does not match windows in Windows 3.1." Lookahead does not occupy characters, that is, when a match occurs, the next matched search follows the previous match, not the word end-of-file that makes up the lookahead.
*   (?! Pattern) performs a subexpression of a reverse lookahead search that matches a search string that is not at the starting point of a string that matches pattern. It is a non capture match, that is, a match cannot be captured for later use. For example, "Windows (?! 752 98| Nt| 2000) "matches windows in Windows 3.1, but does not match windows in Windows 2000." Lookahead does not occupy characters, that is, when a match occurs, the next matched search follows the previous match, not the word end-of-file that makes up the lookahead.
* x| Y matches x or Y. For example, "z| Food matches "Z" or "food". "(z| f) Ood "and" Zood "or" food "match.
* [XYZ] Character set. Matches any one of the characters contained. For example, "[ABC]" matches "a" in "plain".
* [^XYZ] Reverse character set. Matches any characters that are not included. For example, "[^abc]" matches "P" in "plain".
* [A-z] character range. Matches any character within the specified range. For example, "[A-z]" matches any lowercase letter in the range "a" through "Z".
* [^a-z] Reverse range character. Matches any character that is not in the specified range. For example, "[^a-z]" matches any character that is not in the range "a" through "Z".
*/b matches a word boundary, which is the position between the word and the space. For example, "er/b" matches "er" in "never", but does not match "er" in "verb".
*/b non-word boundary matching. "er/b" matches "er" in "verb", but does not match "er" in "Never".
*/CX matches the control characters indicated by X. For example,/cm matches a control-m or carriage return character. The value of x must be between A-Z or a-Z. If this is not the case, then C is assumed to be the "C" character itself.
*/d numeric character matching. equivalent to [0-9].
*/d non-numeric character matching. is equivalent to [^0-9].
*/F page break match. Equivalent to/x0c and/CL.
*/n line-feed character matching. Equivalent to/x0a and/CJ.
*/R matches a return character. Equivalent to/x0d and/cm.
*/s matches any white space characters, including spaces, tabs, page breaks, and so on. is equivalent to [/f/n/r/t/v].
*/S matches any non-white-space character. equivalent to [^/f/n/r/t/v].
*/T tab matching. is equivalent to/x09 and/ci.
*/V vertical tab matching. is equivalent to/x0b and/ck.
*/w matches any character, including underscores. is equivalent to "[a-za-z0-9_]".
*/w any non word character matching. is equivalent to "[^a-za-z0-9_]".
*/XN matches N, where N is a hexadecimal escape code. The hexadecimal escape code must be exactly two digits long. For example, "/x41" matches "A". "/x041" is equivalent to "/x04" & "1". Allows ASCII code to be used in regular expressions.
*/num matches num, where num is a positive integer. The reverse reference to the capture match. For example, "(.) /1 "matches two consecutive identical characters."
*/n identifies a octal escape code or a reverse reference. If there are at least n catch subexpression in front of/n, then n is a reverse reference. Otherwise, if n is the octal number (0-7), then N is the octal escape code.
*/NM identifies a octal escape code or a reverse reference. NM is a reverse reference if there are at least nm catch subexpression in front of the/nm. If there are at least N captures in front of the/nm, then n is the reverse reference, followed by M. If none of the preceding conditions exist,/nm matches octal escape code NM when n and m are octal numbers (0-7).
*/NML matches octal escape code NML when n is a octal number (0-3), M and L are octal number (0-7).
*/un matches N, where N is a Unicode character represented in four-bit hexadecimal numbers. For example,/u00a9 matches the copyright symbol (/U00A9).
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  */

Main function
function Checkform (oform)
{
var els = oform.elements;
Traverse all table elements
for (var i = 0; I < Els.length;i + +)
{
Whether you need to verify
if (Els[i].check)
{
Gets the regular string of validation
var sreg = Els[i].check;
Gets the value of the form, using the universal value function
var sval = GetValue (Els[i]);
String-> regular expression, case-insensitive
var reg = new RegExp (Sreg, "I");
if (! reg.test (Sval))
{
Validation does not pass, pop-up prompts warning
alert (els[i].warning);
This form element gets the focus, with the universal return function
GoBack (Els[i])
return false;
}
}
}
}

The universal value function is divided into three classes to take the value
(1) Text input box, direct value El.value
(2) Tando, traverse all the options, get the number of selected, return the result "00" to select two
(3) Single multiple Pull-down menu, traverse all the options, get the number of selected, return the result "0" indicates that a
function GetValue (EL)
{
To get the type of a FORM element
var stype = El.type;
Switch (stype)
{
Case "Text":
Case "hidden":
Case "Password":
Case "File":
Case "textarea": return el.value;
Case "checkbox":
Case "Radio": Return Getvaluechoose (EL);
Case "Select-one":
Case "Select-multiple": Return Getvaluesel (EL);
}
Get the number of Radio,checkbox selected, with "0" to indicate the number of selected, we write regular time can be passed 0{1,} to indicate the number of selected
function Getvaluechoose (EL)
{
var svalue = "";
Gets the name of the first element and searches for this element group
var tmpels = Document.getelementsbyname (el.name);
for (var i = 0; I < Tmpels.length;i + +)
{
if (tmpels[i].checked)
{
svalue + = "0";
}
}
return svalue;
}
Get the select number of select, with "0" to indicate the number of selected, we write the regular time can pass 0{1,} to indicate the number of selected
function Getvaluesel (EL)
{
var svalue = "";
for (var i = 0; I < El.options.length;i + +)
{
The Select Drop-down Box Prompt option is set to Value= ""
if (el.options[i].selected && el.options[i].value!= "")
{
svalue + = "0";
}
}
return svalue;
}
}

A universal return function that verifies the effect of not passing the return. Three classes to take the value
(1) Text input box, cursor positioned at the end of the text input box
(2) Tando, the first option to gain focus
(3) Single multi Pull-down menu, get focus
function GoBack (EL)
{
To get the type of a FORM element
var stype = El.type;
Switch (stype)
{
Case "Text":
Case "hidden":
Case "Password":
Case "File":
Case "textarea": El.focus (); var rng = El.createtextrange (); Rng.collapse (FALSE); Rng.select ();
Case "checkbox":
     case   "Radio":  var  els  =  document.getelementsbyname (el.name); els[</

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.