JavaScript Form validation Function Pop-up dialog form _javascript tips

Source: Internet
Author: User
Tags first string
Copy Code code as follows:

/**
* Get Chinese and English characters
* @param {} str
* @return {}
*/
function LENGTHB (str) {
var p1 = new RegExp ('%u ... ', ' g ')
var P2 = new RegExp ('%. ', ' G ')
return Escape (str). replace (P1, '). Replace (P2, '). length
}
/**
* Filter all whitespace characters
* @param {Object} str
*/
function Jstrim (str) {
STR + + "";
while ((Str.charat (0) = = ") | | (Str.charat (0) = = '??? ') || (Escape (Str.charat (0)) = = '%u3000 ')
str = str.substring (1, str.length);
while ((Str.charat (str.length-1) = = ") | | (Str.charat (str.length-1) = '??? ') || (Escape (Str.charat (str.length-1)) = = '%u3000 ')
str = str.substring (0, str.length-1);
return str;
}
/**
* Filter Intermediate string and multiple spaces
* @param {Object} inputstring
*/
function Trim (inputstring) {
if (typeof inputstring!= "string") {
return inputstring;
}
var retvalue = inputstring;
var ch = retvalue.substring (0, 1);
while (ch = = "") {
Check the space at the beginning of the string
RetValue = retvalue.substring (1, retvalue.length);
ch = retvalue.substring (0, 1);
}
ch = retvalue.substring (retvalue.length-1, retvalue.length);
while (ch = = "") {
Check for spaces in end of string
RetValue = retvalue.substring (0, retvalue.length-1);
ch = retvalue.substring (retvalue.length-1, retvalue.length);
}
while (Retvalue.indexof ("")!=-1) {
Make multiple contiguous spaces in the middle of text into a single space
RetValue = retvalue.substring (0, Retvalue.indexof ("")) + retvalue.substring (Retvalue.indexof ("") + 1, retValue.length) ;
}
return retvalue;
}
/**
* Filter the string, specify the filter content, and if the content is empty, the default filter ' ~!@#$%^&* ()-+.
* @param {Object} str
* @param {Object} filterstr
*
* @return contains filtered content, returns True, otherwise returns false;
*/
function Filterstr (str, FILTERSTR) {
Filterstr = Filterstr = = ""? " ~!@#$%^&* ()-+.\ "": filterstr
var ch;
var i;
var temp;
var error = false;//Returns True when illegal characters are included
for (i = 0; I <= (filterstr.length-1); i++) {
ch = filterstr.charat (i);
temp = str.indexof (CH);
if (temp!=-1) {
Error = TRUE;
Break
}
}
return error;
}
/**
* Filter the specified content string
* @param {Object} str check string
* @param {Object} filterstr filter string, the content is empty, the default filter ' ~!@#$%^&* ()-+. '
* @param {Object} alertstr Popup Dialog content
* @param {Object} idstr error returns a typo segment ID
*/
function Isfilterstr (str, FILTERSTR, ALERTSTR, idstr) {
Alertstr = "Sorry, you enter" + Alertstr + "does not contain" + filterstr + "illegal character";
if (Filterstr (str, FILTERSTR))
This. Alertandrfalse (Alertstr, IDSTR);
}
/**
* Check whether the URL
* @param {} Str_url
* @param {} alertstr popup field contents
* @param {} idstr cursor positioned field id<b> can only receive id</b>
* @return {Boolean} <b> not </b> URL returns false;
*/
function Isurl (Str_url, Alertstr, idstr) {//Verify URL
Alertstr = alertstr + "incorrectly formatted!";
var Strregex = "^ (HTTPS|HTTP|FTP|RTSP|MMS)?:/ /)" +
"? ([0-9a-z_!~* ' (). &=+$%-]+:)? [0-9a-z_!~* ' (). &=+$%-]+@)? " user@ of FTP
+
"([0-9]{1,3}\.) {3} [0-9] {1,3} "//IP form of URL-199.194.52.184
+
"|"//Allow IP and domain (domains)
+
"([0-9a-z_!~* ' ()-]+\.) * "//Domain name-www."
+
"([0-9a-z][0-9a-z-]{0,61})?" [0-9a-z]\.] Level two domain name
+
[a-z]{2,6}]///the domain-. com or. Museum
+
"(: [0-9]{1,4}]?"//Port-: 80
+
"((/?)|" A slash isn ' t required if there is no file name
+
"(/[0-9a-z_!~* ' ().;?: @&=+$,%#-]+) +/?" $";
var re = new RegExp (Strregex);
if (!re.test (Str_url))
This. Alertandrfalse (Alertstr, IDSTR);
}
/**
* Check to see if email
* @param {} str
* @param {} alertstr popup field contents
* @param {} idstr cursor positioned field id<b> can only receive id</b>
* @return {Boolean} <b> not </b> e-mail returns false;
*/
function Isemail (str, ALERTSTR, IDSTR) {
Alertstr = alertstr + "incorrectly formatted!";
var re =/^ ([a-za-z0-9]+[_|\-|\.]?) *[a-za-z0-9]+@ ([a-za-z0-9]+[_|\-|\.]?) *[a-za-z0-9]+\. [A-za-z] {2,3}$/;
if (!re.test (str))
This. Alertandrfalse (Alertstr, IDSTR);
}
/**
* Check whether it is a number
* @param {} str
* @param {} alertstr popup field contents
* @param {} idstr cursor positioned field id<b> can only receive id</b>
* @return {Boolean} <b> not </b> number returns false;
*/
function Isnum (str, ALERTSTR, IDSTR) {
Alertstr = Alertstr + "should be made up of numbers!";
var re =/^[\d]+$/
if (!re.test (str))
This. Alertandrfalse (Alertstr, IDSTR);
}
/**
* Check whether the value is within a given range <br>
* @param {} str_num
* @param {} Morelen should be greater than or equal to the value
* @param {} Lesslen should be less than or equal to the value
* @param {} alertstr popup field contents
* @param {} idstr cursor positioned field id<b> can only receive id</b>
* @return {Boolean} <b> is less than the minimum value or greater than the maximum value </b> number returns false;
*/
function Israngenum (Str_num, Morelen, Lesslen, Alertstr, idstr) {
Isnum (Str_num, Alertstr, IDSTR);
if (Morelen!= "") {
Alertstr = Alertstr + "value can not be less than" + Morelen;
if (Str_num < Morelen)
This. Alertandrfalse (Alertstr, IDSTR);
}
if (Lesslen!= "") {
Alertstr = Alertstr + "Value cannot be greater than" + Lesslen;
if (Str_num > Lesslen)
This. Alertandrfalse (Alertstr, IDSTR);
}
if (Morelen = = "" && Lesslen = "")
This. Alertandrfalse ("No maximum minimum length is defined!", idstr);
}
/**
* Check for qualified strings (case-insensitive) <br>
* 6 to 20 bits in length, and is a string composed of a-z0-9_
* @param {} str check string
* @param {} alertstr popup field contents
* @param {} idstr cursor positioned field id<b> can only receive id</b>
* @return {Boolean} <b> not </b> "6 to 20 bits in length and a-z0-9_" returns false;
*/
function islicit (str, ALERTSTR, IDSTR) {
Alertstr = "Sorry," + Alertstr + "cannot be empty and can only be composed of 6 to 20 bits from 0 to 9 A to Z underline!";
var re =/^[_0-9a-za-z]{6,20}$/
if (!re.test (str))
This. Alertandrfalse (Alertstr, IDSTR);
}
/**
* Check whether two strings are equal
* @param {} str1 First string
* @param {} str2 Second string
* @param {} alertstr popup field contents
* @param {} idstr cursor positioned field id<b> can only receive id</b>
* @return {Boolean} string is not equal returns false;
*/
function Isstrequals (str1, str2, Alertstr, idstr) {
Alertstr = "two times" + Alertstr + "inconsistent!";
if (str1!= str2)
This. Alertandrfalse (Alertstr, IDSTR);
}
/**
* Check whether the string is within a given length (Chinese characters are calculated in 2 bytes) <br>
*
* @param {} str checked characters
* @param {} Morelen should be greater than or equal to the length
* @param {} Lesslen should be less than or equal to the length
* @param {} alertstr popup field contents
* @param {} idstr cursor positioned field id<b> can only receive id</b>
* @return {Boolean} <b> less than minimum length or greater than maximum length </b> number returns false;
*/
function Isrange (str, Morelen, Lesslen, Alertstr, idstr) {
var strLen = LENGTHB (str);
if (Morelen!= "") {
Alertstr = Alertstr + "should be greater than or equal to" + Morelen + "byte length!";
if (StrLen < Morelen)
This. Alertandrfalse (Alertstr, IDSTR);
}
if (Lesslen!= "") {
Alertstr = Alertstr + "should be less than or equal to" + Lesslen + "byte length!";
if (StrLen > Lesslen)
This. Alertandrfalse (Alertstr, IDSTR);
}
if (Morelen = = "" && Lesslen = "")
This. Alertandrfalse ("No maximum minimum length is defined!", idstr);
}
/**
* Check whether the string is less than the given length range (Chinese characters are calculated in 2 bytes) <br>
* @param {} str string
* @param {} Lesslen less than or equal to length
* @param {} alertstr popup field contents
* @param {} idstr cursor positioned field id<b> can only receive id</b>
* @return {Boolean} <b> greater than the given length </b> number returns false;
*/
function isless (str, Lesslen, ALERTSTR, idstr) {
Isrange (str, "", Lesslen, Alertstr, IDSTR);
}
/**
* Check the character is not empty
* @param {} str
* @param {} alertstr popup field contents
* @param {} idstr cursor positioned field id<b> can only receive id</b>
* @return {Boolean} <b> character is null </b> returns false;
*/
function IsEmpty (str, ALERTSTR, IDSTR) {
Alertstr = Alertstr + "cannot be empty!";
if (str = "")
This. Alertandrfalse (Alertstr, IDSTR);
}
/**
* Pop-up warning box, and positioning to the wrong section of the typo
* @param {} alertstr pop-up warning box content
* @param {} idstr return field focus
* @return {Boolean} returns false
*/
function Alertandrfalse (ALERTSTR, idstr) {
alert (ALERTSTR);
document.getElementById (IDSTR). focus ();
Throw "Valueerr";
}
JavaScript Document
Call Example
function Checkform () {
try {
var title = Trim (document.getElementById (' title ')); Article title
IsEmpty (title, "article title", "title");/NOT NULL
Isless (title,100, "article title", "title");//title should be less than 100 characters in length
Isfilterstr (title, "~"!@#$%^&* ()-=_+[]{}|\\; ': \ ",./<>?", "article title", "title")//title not allowed to contain these illegal characters
} catch (Err) {
if (err = = "Valueerr")
return false;
}
}
Related Article

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.