Use JavaScript to determine dates, numbers, integers, and special characters

Source: Internet
Author: User
Tags date format contains eval execution integer numeric value tostring
Javascript| date How to use a program to detect the input of the content is legitimate, the following is my own program to write the date, number, integer and special characters are also judged to be positive or negative (for numeric classes and integers)

< script language= "JavaScript" >
<!--


/**
Logicalvalue: Used to determine whether the object's value meets the criteria, the options available are:
Integer: Integral type, also can judge positive integer and negative integral type
Number: Numerical type, also can be judged positive and negative
Date: Date format, which can be supported by a custom separator, with the default ´-´ as separator
String: Determining whether a string includes or excludes certain characters
return value:
True or False

Parameters:
Objstr: Object identifier--object name;
ObjType: Object Type (´integer´,´number´,´date´,´string´ one)

Other Notes:
An error is returned when the object value is empty.

Author:ppdj

Example:
Example 1: Required to verify that the input box Text1 input data is "integer" data, if not, then prompt
if (! Logicalvalue (´text1´,´integer´)) alert (´error:your must input A-integer Number´);
Example 2: Required to verify that the input box Text1 input data is "positive integer" data, if not, then prompt
if (! Logicalvalue (´text1´,´integer´,´+´)) alert (´error:your must input a positive integer number´);
Example 3: Required to check the input box Text1 input data is "negative integer" data, if not, then prompt
if (! Logicalvalue (´text1´,´integer´,´-´)) alert (´error:your must input a negative integer number´);
Exmaple 4: Required to verify the input box Text1 input data is a numeric value, if not, then prompt
if (! Logicalvalue (´text1´,´number´)) alert (´error:your must input a number´);
Exmaple 5: Required to verify the input box Text1 input data is a "positive" value, if not, then prompt
if (! Logicalvalue (´text1´,´number´,´+´)) alert (´error:your must input a number´);
Exmaple 6: Required to verify the input box Text1 input data is a "negative" value, if not, then prompt
if (! Logicalvalue (´text1´,´number´,´-´)) alert (´error:your must input a number´);
Example 7: Required to verify the input box Text1 input data is the date type, if not, then prompt
if (! Logicalvalue (´text1´,´date´) alert (´error:your must input a logical date Value´);
If its delimiter is a, "a" is a variable, such as the commonly used ´-´ and ´/´, the following syntax
if (! Logicalvalue (´text1´,´date´,a) alert (´error:your must input A logical date Value´);
Or when the delimiter is ´/´
if (! Logicalvalue (´text1´,´date´,´/´) alert (´error:your must input a logical date Value´);
When the separator is ´-´, you can not ´-´ the parameter, you can take
Example 8: Required to check the input box Text1 input to indicate the color of the string is reasonable, if not reasonable, then prompt
if (! Logicalvalue (´text1´,´string´,´0123456789abcdefabcdef´) alert (´error:your must input a logical color Value´);
Example 9: Required to verify the input box Text1 input indicates whether the password string does not contain "´" @#$%&^* "These characters, if it contains, then prompts
if (! Logicalvalue (´text1´,´string´,´´ "@#$%&^*´,false") alert (´error:your password can not Contain´ "@#$%&^*´");
Where false indicates that some characters are not included, true indicates which characters must be, and the default value is True
*/
function Logicalvalue (objstr,objtype)
{
var Str=´´;
if ((objstr==null) | | (Objstr==´´) | | Objtype==null)
{
Alert (´ function logicalvalue missing parameter ´);
return false;
}
var obj = document.all (OBJSTR);
if (Obj.value==´´) return false;
for (Var i=2;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 does not provide ´ in the current version);
return false;
}
}

/**
Isinteger: Used to determine whether a numeric string is an integer,
You can also determine whether a positive or negative integer returns a value of TRUE or False
String: strings to be judged
Sign: To judge is a positive negative number is used, is being used ´+´, negative ´-´, do not say no judgment
Author:ppdj
Sample
var a =´123´;
if (Isinteger (a))
{
Alert (´a is a integer´);
}
if (Isinteger (A,´+´))
{
Alert (A is a positive integer);
}
if (Isinteger (A,´-´))
{
Alert (´a is a negative integer´);
}
*/

function Isinteger (string, sign)
{
var integer;
if ((sign!=null) && (Sign!=´-´) && (Sign!=´+´))
{
An error occurred in the parameter of alert (´isinter string,sign): sign 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: A string used to determine whether a string is a date format

return value:
True or False

Parameters:
DateString: A string to judge
Dilimeter: The delimiter for the date, the default value is ´-´

Author:ppdj
Sample
var date =´1999-1-2´;
if (IsDate (date))
{
Alert (´you, the default separator is "-");
}
Date =´1999/1/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+´8:0:0´;//Plus eight hours is because we're in the East eight area.
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 a numeric type,
You can also determine whether a positive or negative number returns a value of TRUE or False
String: strings to be judged
Sign: To judge is a positive negative number is used, is being used ´+´, negative ´-´, do not say no judgment
Author:ppdj
Sample
var a =´123´;
if (Isnumber (a))
{
Alert (´a is a number´);
}
if (Isnumber (A,´+´))
{
Alert (A is a positive number);
}
if (Isnumber (A,´-´))
{
Alert (´a is a negative number´);
}
*/

function Isnumber (string,sign)
{
var number;
if (string==null) return false;
if ((sign!=null) && (Sign!=´-´) && (Sign!=´+´))
{
An error occurred in the parameter of alert (´isnumber string,sign): sign 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: strings to be judged
Compare: Comparison of strings (datum strings)
Belongornot:true or False, "true" means that each character of a string is contained in compare.
' false ' means that each character of a string is not included in the Compare

Author:ppdj
Sample1:
var str =´123g´;
if (specialstring (Str,´1234567890´))
{
Alert (´yes, all the letter of the string In´1234567890´´);
}
Else
{
Alert (´no, one or more letters of the string not In´1234567890´´);
}
Result is the else part of the execution
Sample2:
var password =´1234´;
if (! Specialstring (Password,´´ "@#$%´,false)")
{
Alert (´yes, the password is Correct.´);
}
Else
{
Alert (´no, the password is contain one or more letters Of´ "@#$%´´);
}
Result is the else part of the execution
*/
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{
if (Compare.indexof (String.charat (i)) ==-1)
return False
}
return true;
}
Else
{
for (Var i=0;i{
if (Compare.indexof (String.charat (i))!=-1)
return False
}
return true;
}
}


-->
</script>

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.