How can I use a program to check whether the entered content is legal, the following is a self-developed program that judges the date, number, integer, and special character, and can also determine whether it is a positive number or a negative number (for the value class and integer)
<Script language = "JavaScript">
<! --
/**
Logicalvalue: used to determine whether the object value meets the condition. Available options include:
Integer: integer. It can also be used to determine positive and negative integers.
Number: numeric type, which can also determine Positive and Negative
Date: date type. It supports date format with custom delimiters. The default value is '-'.
String: determines whether a string contains or does not contain certain characters.
Return Value:
True or false
Parameters:
Objstr: Object Identifier-Object Name;
Objtype: object type ('integer', 'number', 'date', one of 'string)
Other Instructions:
If the object value is null, an error is returned.
Author: ppdj
Example:
Example 1: Check whether the input data of text1 is "integer". If not, the system prompts
If (! Logicalvalue ('text1', 'integer') Alert ('error: Your must input a integer number ');
Example 2: Check whether the input data of text1 is a positive integer. If not, a prompt is displayed.
If (! Logicalvalue ('text1', 'integer', '+') Alert ('error: Your must input a positive integer number ');
Example 3: Check whether the input data of text1 is "negative integer". If not, the system prompts
If (! Logicalvalue ('text1', 'integer', '-') Alert ('error: Your must input a negative integer number ');
Exmaple 4: Check whether the input data of text1 is a numerical value. If not, a prompt is displayed.
If (! Logicalvalue ('text1', 'number') Alert ('error: Your must input a number ');
Exmaple 5: Check whether the input data of text1 is a positive value. If not, the system prompts
If (! Logicalvalue ('text1', 'number', '+') Alert ('error: Your must input a number ');
Exmaple 6: Check whether the input data of text1 is a negative value. If not, the system prompts
If (! Logicalvalue ('text1', 'number', '-') Alert ('error: Your must input a number ');
Example 7: Check whether the input data in text1 is of the date type. If not, a prompt is displayed.
If (! Logicalvalue ('text1', 'date') Alert ('error: Your must input a logical date value ');
If its separator is a and "A" is a variable, such as '-' and '/', the following syntax is used:
If (! Logicalvalue ('text1', 'date', A) Alert ('error: Your must input a logical date value ');
Or when the separator is '/'
If (! Logicalvalue ('text1', 'date', '/') Alert ('error: Your must input a logical date value ');
When the separator is '-', do not set the parameter '-'.
Example 8: Check whether the input in text1 indicates that the color string is reasonable. If not, a prompt is displayed.
If (! Logicalvalue ('text1', 'string', '0123456789abcdefabcdef ') Alert ('error: Your must input a logical color value ');
Example 9: Check whether the character string "'" @ # $ % & ^ * "is not included in the input text box text1. If the character string contains, a prompt is displayed.
If (! Logicalvalue ('text1', 'string', '\' "#$ % & ^ * ', false) Alert ('error: your password can not contain \ '"#$ % & ^ *');
"False" indicates that some characters are not included, and "true" indicates which characters must be included. The default value is "true ".
*/
Function logicalvalue (objstr, objtype)
{
VaR STR = '';
If (objstr = NULL) | (objstr = '') | objtype = NULL)
{
Alert ('parameter missing for function logicalvalue ');
Return false;
}
VaR OBJ = Document. All (objstr );
If (obj. value = '') return false;
For (VAR I = 2; I <arguments. length; I ++)
{
If (STR! = '')
STR + = ',';
STR + = 'arguments ['+ I +'] ';
}
STR = (STR = ''? 'Obj. value': 'obj. value, '+ Str );
VaR temp = objtype. tolowercase ();
If (temp = 'integer ')
{
Return eval ('isinteger ('+ STR + ')');
}
Else if (temp = 'number ')
{
Return eval ('isnumber ('+ STR + ')');
}
Else if (temp = 'string ')
{
Return eval ('specialstring ('+ STR + ')');
}
Else if (temp = 'date ')
{
Return eval ('isdate ('+ STR + ')');
}
Else
{
Alert ('"' + temp + '" type not provided in current version ');
Return false;
}
}
/**
Isinteger: used to determine whether a numeric string is an integer,
You can also determine whether it is a positive integer or a negative integer. the return value is true or false.
String: the string to be determined.
Sign: If you want to determine whether it is positive or negative, it is positive + ', negative'-'. If not, it indicates no judgment.
Author: ppdj
Sample:
VaR A = '20140901 ';
If (isinteger ())
{
Alert ('a is a integer ');
}
If (isinteger (A, '+ '))
{
Alert (A is a positive integer );
}
If (isinteger (,'-'))
{
Alert ('a is a negative integer ');
}
*/
Function isinteger (string, sign)
{
VaR integer;
If (sign! = NULL) & (sign! = '-') & (Sign! = '+ '))
{
Alert ('isinter (string, sign) parameter error: \ nsign is null or "-" or "+ "');
Return false;
}
Integer = parseint (string );
If (isnan (integer ))
{
Return false;
}
Else if (integer. tostring (). Length = string. length)
{
If (Sign = NULL) | (Sign = '-' & integer <0) | (Sign = '+' & integer> 0 ))
{
Return true;
}
Else
Return false;
}
Else
Return false;
}
/**
Isdate: used to determine whether a string is a string in Date Format
Return Value:
True or false
Parameters:
Datestring: string to be judged
Dilimeter: delimiter of the date. The default value is '-'
Author: ppdj
Sample:
VaR date = '2014-1-2 ';
If (isdate (date ))
{
Alert ('You see, the default separator is "-");
}
Date = '2014/2 ";
If (isdate (date ,'/'))
{
Alert ('the date \'s separator is "/");
}
*/
Function isdate (datestring, dilimeter)
{
If (datestring = NULL) return false;
If (dilimeter = ''| dilimeter = NULL)
Dilimeter = '-';
VaR Tempy = '';
VaR tempm = '';
VaR tempd = '';
VaR temparray;
If (datestring. Length <8 & datestring. length> 10)
Return false;
Temparray = datestring. Split (dilimeter );
If (temparray. length! = 3)
Return false;
If (temparray [0]. Length = 4)
{
Tempy = temparray [0];
Tempd = temparray [2];
}
Else
{
Tempy = temparray [2];
Tempd = temparray [1];
}
Tempm = temparray [1];
VaR tdatestring = Tempy + '/' + tempm + '/' + tempd + '0'; // Add eight hours because we are in the UTC + 8 zone.
VaR tempdate = new date (tdatestring );
If (isnan (tempdate ))
Return false;
If (tempdate. getutcfullyear ()). tostring () = Tempy) & (tempdate. getmonth () = parseint (tempm)-1) & (tempdate. getdate () = parseint (tempd )))
{
Return true;
}
Else
{
Return false;
}
}
/**
Isnumber: used to determine whether a numeric string is Numeric,
You can also determine whether it is a positive or negative number. The return value is true or false.
String: the string to be determined.
Sign: If you want to determine whether it is positive or negative, it is positive + ', negative'-'. If not, it indicates no judgment.
Author: ppdj
Sample:
VaR A = '20140901 ';
If (isnumber ())
{
Alert ('a is a number ');
}
If (isnumber (A, '+ '))
{
Alert (A is a positive number );
}
If (isnumber (,'-'))
{
Alert ('a is a negative number ');
}
*/
Function isnumber (string, sign)
{
VaR number;
If (string = NULL) return false;
If (sign! = NULL) & (sign! = '-') & (Sign! = '+ '))
{
Alert ('isnumber (string, sign) parameter error: \ nsign is null or "-" or "+ "');
Return false;
}
Number = new number (string );
If (isnan (number ))
{
Return false;
}
Else if (Sign = NULL) | (Sign = '-' & number <0) | (Sign = '+' & number> 0 ))
{
Return true;
}
Else
Return false;
}
/**
Specialstring: used to determine whether a string contains or does not contain certain characters.
Return Value:
True or false
Parameters:
String: the string to be determined.
Compare: Comparison string (benchmark string)
Belongornot: true or false. "True" indicates that every character of string is included in compare,
"False" indicates that every character of string is not included in compare.
Author: ppdj
Sample1:
VaR STR = '123g ';
If (specialstring (STR, '20140901 '))
{
Alert ('yes, all the letter of the string in \ '000000 \'');
}
Else
{
Alert ('no, one or more letters of the string not in \ '000000 \'');
}
The result is the else part.
Sample2:
VaR Password = '000000 ';
If (! Specialstring (password, '\' "@ # $ % ', false ))
{
Alert ('yes, the password is correct .');
}
Else
{
Alert ('no, the password is contain one or more letters of \ '"#% % \'');
}
The result is the else part.
*/
Function specialstring (string, compare, belongornot)
{
If (string = NULL) | (compare = NULL) | (belongornot! = NULL) & (belongornot! = True) & (belongornot! = False )))
{
Alert ('function specialstring (string, compare, belongornot) parameter error ');
Return false;
}
If (belongornot = NULL | belongornot = true)
{
For (VAR I = 0; I <string. length; I ++)
{
If (compare. indexof (string. charat (I) =-1)
Return false
}
Return true;
}
Else
{
For (VAR I = 0; I <string. length; I ++)
{
If (compare. indexof (string. charat (I ))! =-1)
Return false
}
Return true;
}
}
// -->
</SCRIPT>