JS validation scripts often used

Source: Internet
Author: User
To share with you often use the JS verification script, very practical, like to collect it.

1//================================================================
2//This function is used to limit the number "0" to "9" in the text input box, ".", "-"
3//================================================================
4 function Jhshnumbertext ()
5 {
6 if (!) ( ((Window.event.keyCode >=) && (Window.event.keyCode <= 57))
7 (Window.event.keyCode = Window.event.keyCode = 46)
8 (Window.event.keyCode = 45))
9 {
Ten window.event.keyCode = 0;
11}
//<form name=frm>
//<input type=text name=test value= "onkeypress=" Jhshnumbertext () ">
//<input Type=button Name=submit value=submit>
//</form>
16}
17
18//================================================================
19//This function is used to automatically convert the contents of the input text box into uppercase characters
20//================================================================
function Jhshtouppercase ()
22 {
if ((Window.event.keyCode >=) && (window.event.keyCode <= 122))
24 {
Window.event.keyCode = window.event.keycode-32;
26}
27}
28
29//================================================================
30//This function is used to automatically convert the contents of the Input text box to lowercase characters
31//================================================================
Jhshtolowercase function ()
33 {
if ((Window.event.keyCode >=) && (Window.event.keyCode <= 90))
35 {
Window.event.keyCode = Window.event.keyCode + 32;
37}
38}
39
40///the space before and after the truncation string
The Jhshtrim function (sstring)
42 {
strtmp var;
strtmp = Jhshrtrim (Jhshltrim (sstring));
Strtmp return;
46}
47
/-----------------------------------------------------------------------------------
49//This function is used to truncate the sstring string before a space
/-----------------------------------------------------------------------------------
Wuyi function Jhshltrim (sstring)
52 {
The var sstr,i,istart,sresult = "";
54
SSTR = Sstring.split ("");
IStart =-1;
for (i = 0; i < sstr.length; i++)
58 {
(Sstr[i]!= "")
60 {
IStart = i;
a break;
63}
64}
(IStart = = 1)
{return "";} Indicates that all characters in the sstring are spaces, then returns an empty string
Or else
{return sstring.substring (IStart);}
69}
70
A//-----------------------------------------------------------------------------------
72//This function is used for trailing space truncation of the sstring string
I//-----------------------------------------------------------------------------------
The Jhshrtrim function (sstring)
75 {
The var sstr,i,istart,sresult = "";
SSTR = Sstring.split ("");
IStart =-1;
for (i = sstr.length-1 i >= 0; i--)
80 {
Bayi if (Sstr[i]!= "")
82 {
IStart = i;
The break;
85}
86}
87
if (IStart = 1)
{return "";} Indicates that all characters in the sstring are spaces, then returns an empty string
Or else
sstring.substring {return (0,istart+1);}
92}
93
94
/------------------------------------------------------------------------------------
96//To determine whether the string is a date format,//Standard format: 2004-3-29 12:05 No other formats are allowed
A/------------------------------------------------------------------------------------
IsDate function (datestring)
99 {
if (datestring = null)
The return false;
102
Dilimeter var = '-';
var tempy = ';
The var tempm = ';
The tempd var = ';
107
108 var temph = ';
109 var tempmi = ';
The var TempS = ';
111
112 Var Temparr1,temparr2,temparr3;
113 if (Datestring.length < 8 && datestring.length > 18)
114 return false;
TEMPARR1 = Datestring.split ("");
116 if (Temparr1.length!= 2)
117 return false;
118
119 TEMPARR2 = Temparr1[0].split (Dilimeter);
if (temparr2.length!= 3)
121 return false;
122 tempy = temparr2[0];
123 tempm = temparr2[1];
124 tempd = temparr2[2];
125
126 TEMPARR3 = Temparr1[1].split (': ');
127 if (Temparr2.length < 2 temparr2.length > 3)
128 {
129 return false;
130}
131 temph = temparr3[0];
132 TEMPMI = temparr3[1];
(Temparr2.length = 3)
134 TempS = temparr3[2];
135
136 Var tdatestring;
137 if (temparr3.length = 2)
138 {
139 tdatestring = Tempy + '/' + tempm + '/' +tempd + ' +temph+ ': ' +tempmi;
140}
A.
142 {
143 tdatestring = Tempy + '/' + tempm + '/' +tempd + ' +temph+ ': ' +tempmi+ ': ' +temps;
144}
145 var tempdate = new Date (tdatestring);
146 if (isNaN (tempdate))
147 {
148//alert ("isNaN");
149 return false;
150}
151
152 if (Temparr3.length = 2)
153 {
154//2004-3-29 12:05
(Tempdate.getutcfullyear (). toString () = Tempy) &&
156 (Tempdate.getmonth () = parseint (TEMPM)-1) &&
157 (tempdate.getdate () = = parseint (TEMPD)) &&
158 (tempdate.gethours () = = parseint (temph)) &&
159 (tempdate.getminutes () = = parseint (TEMPMI))
160 {
161 return true;
162}
163 Else
164 {
165 return false;
166}
167}
Or else
169 {
170//2004-3-29 12:05:30
171 if (Tempdate.getutcfullyear (). toString () = Tempy) &&
172 (tempdate.getmonth () = parseint (TEMPM)-1) &&
173 (tempdate.getdate () = = parseint (TEMPD)) &&
174 (tempdate.gethours () = = parseint (temph)) &&
175 (tempdate.getminutes () = = parseint (TEMPMI)) &&
176 (tempdate.getseconds () = = parseint (TempS))
177 {
178 return true;
179}
180 Else
181 {
The return false;
183}
184}
185
186}
187
188//------------------------------------------------------------------------------------
189//Judge string date size,//Standard format: 2004-3-29 12:05
190//Use the validity of the date before the judgement
191//If DateString1 is later than DateString2, returns true;
A/------------------------------------------------------------------------------------
193
194 function Datecompare (DateString1, DateString2)
195 {
196 var i;
197 var dilimeter = '-';
198
199 var temparr1,temparr2;
TEMPARR1 = Datestring1.split (Dilimeter);
201 TEMPARR2 = Datestring2.split (Dilimeter);
202
203 var tDateString1 = "";
204 for (i=0; i<temparr1.length; i++)
205 {
206 if (I < temparr1.length-1)
207 TDateString1 + = temparr1[i]+ "/";
Or else
209 TDateString1 + + temparr1[i];
210}
211 var tDateString2 = "";
212 for (i=0; i<temparr2.length; i++)
213 {
214 if (I < temparr2.length-1)
215 TDateString2 + = temparr2[i]+ "/";
216 Else
217 tDateString2 + + temparr2[i];
218
219}
220
221 var tempDate1 = new Date (TDATESTRING1);
222 var tempDate2 = new Date (tDateString2);
223 if (isNaN (tempDate1) isNaN (TEMPDATE2))
224 {
return false;
226}
227
228//2004-3-29 12:05:30
229 if (Tempdate1.getutcfullyear () < Tempdate2.getutcfullyear ())
230 return false;
231 if (tempdate1.getutcfullyear () > Tempdate2.getutcfullyear ())
232 return true;
233 if (Tempdate1.getmonth () < Tempdate2.getmonth ())
234 return false;
235 if (Tempdate1.getmonth () > Tempdate2.getmonth ())
236 return true;
237 if (tempdate1.getdate () < Tempdate2.getdate ())
238 return false;
239 if (Tempdate1.getdate () > Tempdate2.getdate ())
The return true;
241 if (tempdate1.gethours () < tempdate2.gethours ())
242 return false;
243 if (tempdate1.gethours () > Tempdate2.gethours ())
244 return true;
245 if (Tempdate1.getminutes () < Tempdate2.getminutes ())
246 return false;
247 if (Tempdate1.getminutes () > Tempdate2.getminutes ())
248 return true;
249 if (Tempdate1.getseconds () < Tempdate2.getseconds ())
return false;
251 if (Tempdate1.getseconds () > Tempdate2.getseconds ())
252 return true;
253//If Equal returns False
254 return false;
255}
256



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.