|
"^\d+$" //非负整数(正整数 + 0) -----------------2 ----------------------- java script验证表单时常用: ----------3---------------- 正则表达式用于字符串处理、表单验证等场合,实用高效。现将一些常用的表达式收集于此,以备不时之需。 |
/* |
用途:检查输入的Email信箱格式是否正确 |
输入:strEmail:字符串 |
返回:如果通过验证返回true,否则返回false |
*/ |
function checkEmail(strEmail) |
{ |
//var emailReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/; |
var emailReg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/; |
if ( emailReg.test(strEmail) ) { |
return true; |
} |
else { |
alert("您输入的Email地址格式不正确!"); |
return false; |
} |
}; |
|
/* |
用途:校验ip地址的格式 |
输入:strIP:ip地址 |
返回:如果通过验证返回true,否则返回false; |
*/ |
function isIP(strIP) |
{ |
if (isNull(strIP)) { |
return false; |
} |
var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g //匹配IP地址的正则表达式 |
if (re.test(strIP)) { |
if ( RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256) { |
return true; |
} |
} |
return false; |
}; |
|
/* |
用途:检查输入手机号码是否正确 |
输入:strMobile:字符串 |
返回:如果通过验证返回true,否则返回false |
*/ |
function checkMobile( strMobile ) |
{ |
var regu = /^[1][3][0-9]{9}$/; |
var re = new RegExp(regu); |
if (re.test(strMobile)) { |
return true; |
} |
else { |
return false; |
} |
}; |
|
/* |
用途:检查输入的电话号码格式是否正确 |
输入:strPhone:字符串 |
返回:如果通过验证返回true,否则返回false |
*/ |
function checkPhone( strPhone ) |
{ |
var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,10}$/; |
var phoneRegNoArea = /^[1-9]{1}[0-9]{5,8}$/; |
var prompt = "您输入的电话号码不正确!" if ( strPhone.length > 9 ) { |
if ( phoneRegWithArea.test(strPhone) ) { |
return true; |
} |
else { |
alert( prompt ); |
return false; |
} |
} |
else { |
if ( phoneRegNoArea.test( strPhone ) ) { |
return true; |
} |
else { |
alert( prompt ); |
return false; |
} |
} |
}; |
|
/* |
用途:检查输入字符串是否为空或者全部都是空格 |
输入:str |
返回:如果全是空返回true,否则返回false |
*/ |
function isNull( str ) |
{ |
if ( str == "" ) { |
return true; |
} |
var regu = "^[ ]+$"; |
var re = new RegExp(regu); |
return re.test(str); |
}; |
|
/* |
用途:检查输入对象的值是否符合整数格式 |
输入:str 输入的字符串 |
返回:如果通过验证返回true,否则返回false |
*/ |
function isInteger( str ) |
{ |
var regu = /^[-]{0,1}[0-9]{1,}$/; |
return regu.test(str); |
}; |
|
/* |
用途:检查输入字符串是否符合正整数格式 |
输入:s:字符串 |
返回:如果通过验证返回true,否则返回false |
*/ |
function isNumber( s ) |
{ |
var regu = "^[0-9]+$"; |
var re = new RegExp(regu); |
if (s.search(re) != - 1) { |
return true; |
} |
else { |
return false; |
} |
}; |
|
/* |
用途:检查输入字符串是否是带小数的数字格式,可以是负数 |
输入:str:字符串 |
返回:如果通过验证返回true,否则返回false |
*/ |
function isDecimal( str ) |
{ |
if (isInteger(str)) { |
return true; |
} |
var re = /^[-]{0,1}(\d+)[\.]+(\d+)$/; |
if (re.test(str)) { |
if (RegExp.$1 == 0 && RegExp.$2 == 0) { |
return false; |
} |
return true; |
} |
else { |
return false; |
} |
}; |
|
/* |
用途:检查输入对象的值是否符合端口号格式 |
输入:str 输入的字符串 |
返回:如果通过验证返回true,否则返回false |
*/ |
function isPort( str ) |
{ |
return (isNumber(str) && str < 65536); |
}; |
|
/* |
用途:检查输入字符串是否符合金额格式,格式定义为带小数的正数,小数点后最多三位 |
输入:s:字符串 |
返回:如果通过验证返回true,否则返回false |
*/ |
function isMoney( s ) |
{ |
var regu = "^[0-9]+[\.][0-9]{0,3}$"; |
var re = new RegExp(regu); |
if (re.test(s)) { |
return true; |
} |
else { |
return false; |
} |
}; |
|
/* |
用途:检查输入字符串是否只由英文字母和数字和下划线组成 |
输入:s:字符串 |
返回:如果通过验证返回true,否则返回false |
*/ |
function isNumberOr_Letter( s ) |
{ |
//判断是否是数字或字母 |
var regu = "^[0-9a-zA-Z\_]+$"; |
185 |
var re = new RegExp(regu); |
186 |
if (re.test(s)) { |
187 |
return true; |
188 |
} |
189 |
else { |
190 |
return false; |
191 |
} |
192 |
}; |
193 |
|
194 |
/* |
195 |
用途:检查输入字符串是否只由英文字母和数字组成 |
196 |
输入:s:字符串 |
197 |
返回:如果通过验证返回true,否则返回false |
198 |
*/ |
199 |
function isNumberOrLetter( s ) |
200 |
{ |
201 |
//判断是否是数字或字母 |
202 |
var regu = "^[0-9a-zA-Z]+$"; |
203 |
var re = new RegExp(regu); |
204 |
if (re.test(s)) { |
205 |
return true; |
206 |
} |
207 |
else { |
208 |
return false; |
209 |
} |
210 |
}; |
211 |
|
212 |
/* |
213 |
用途:检查输入字符串是否只由汉字、字母、数字组成 |
214 |
输入:s:字符串 |
215 |
返回:如果通过验证返回true,否则返回false |
216 |
*/ |
217 |
function isChinaOrNumbOrLett( s ) |
218 |
{ |
219 |
//判断是否是汉字、字母、数字组成 |
220 |
var regu = "^[0-9a-zA-Z\u4e00-\u9fa5]+$"; |
221 |
var re = new RegExp(regu); |
222 |
if (re.test(s)) { |
223 |
return true; |
224 |
} |
225 |
else { |
226 |
return false; |
227 |
} |
228 |
}; |
229 |
|
230 |
/* |
231 |
用途:判断是否是日期 |
232 |
输入:date:日期;fmt:日期格式 |
233 |
返回:如果通过验证返回true,否则返回false |
234 |
*/ |
235 |
function isDate( date, fmt ) |
236 |
{ |
237 |
if (fmt == null) { |
238 |
fmt = "yyyyMMdd"; |
239 |
} |
240 |
var yIndex = fmt.indexOf("yyyy"); |
241 |
if (yIndex ==- 1) { |
242 |
return false; |
243 |
} |
244 |
var year = date.substring(yIndex, yIndex + 4); |
245 |
var mIndex = fmt.indexOf("MM"); |
246 |
if (mIndex ==- 1) { |
247 |
return false; |
248 |
} |
249 |
var month = date.substring(mIndex, mIndex + 2); |
250 |
var dIndex = fmt.indexOf("dd"); |
251 |
if (dIndex ==- 1) { |
252 |
return false; |
253 |
} |
254 |
var day = date.substring(dIndex, dIndex + 2); |
255 |
if (!isNumber(year) || year > "2100" || year < "1900") { |
256 |
return false; |
257 |
} |
258 |
if (!isNumber(month) || month > "12" || month < "01") { |
259 |
return false; |
260 |
} |
261 |
if (day > getMaxDay(year, month) || day < "01") { |
262 |
return false; |
263 |
} |
264 |
return true; |
265 |
}; |
266 |
function getMaxDay(year, month) |
267 |
{ |
268 |
if (month == 4 || month == 6 || month == 9 || month == 11) { |
269 |
return "30"; |
270 |
} |
271 |
if (month == 2) { |
272 |
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { |
273 |
return "29"; |
274 |
} |
275 |
else { |
276 |
return "28"; |
277 |
} |
278 |
return "31";; |
279 |
} |
280 |
}; |
281 |
|
282 |
/* |
283 |
用途:字符1是否以字符串2结束 |
284 |
输入:str1:字符串;str2:被包含的字符串 |
285 |
返回:如果通过验证返回true,否则返回false |
286 |
*/ |
287 |
function isLastMatch(str1, str2) |
288 |
{ |
289 |
var index = str1.lastIndexOf(str2); |
290 |
if (str1.length == index + str2.length) { |
291 |
return true; |
292 |
} |
293 |
return false; |
294 |
}; |
295 |
|
296 |
/* |
297 |
用途:字符1是否以字符串2开始 |
298 |
输入:str1:字符串;str2:被包含的字符串 |
299 |
返回:如果通过验证返回true,否则返回false |
300 |
*/ |
301 |
function isFirstMatch(str1, str2) |
302 |
{ |
303 |
var index = str1.indexOf(str2); |
304 |
if (index == 0) { |
305 |
return true; |
306 |
} |
307 |
return false; |
308 |
}; |
309 |
|
310 |
/* |
311 |
用途:字符1是包含字符串2 |
312 |
输入:str1:字符串;str2:被包含的字符串 |
313 |
返回:如果通过验证返回true,否则返回false |
314 |
*/ |
315 |
function isMatch(str1, str2) |
316 |
{ |
317 |
var index = str1.indexOf(str2); |
318 |
if (index ==- 1) { |
319 |
return false; |
320 |
} |
321 |
return true; |
322 |
}; |
323 |
|
324 |
/* |
325 |
用途:检查输入的起止日期是否正确,规则为两个日期的格式正确,且结束如期>=起始日期 |
326 |
输入:startDate:起始日期,字符串; endDate:结束如期,字符串 |
327 |
返回:如果通过验证返回true,否则返回false |
328 |
*/ |
329 |
function checkTwoDate( startDate, endDate ) |
330 |
{ |
331 |
if ( !isDate(startDate) ) { |
332 |
alert("起始日期不正确!"); |
333 |
return false; |
334 |
} |
335 |
else if ( !isDate(endDate) ) { |
336 |
alert("终止日期不正确!"); |
337 |
return false; |
338 |
} |
339 |
else if ( startDate > endDate ) { |
340 |
alert("起始日期不能大于终止日期!"); |
341 |
return false; |
342 |
} |
343 |
return true; |
344 |
}; |
345 |
|
346 |
/* |
347 |
用途:检查复选框被选中的数目 |
348 |
输入:checkboxID:字符串 |
349 |
返回:返回该复选框中被选中的数目 |
350 |
*/ |
351 |
function checkSelect( checkboxID ) |
352 |
{ |
353 |
var check = 0; |
354 |
var i = 0; |
355 |
if ( document.all(checkboxID).length > 0 ) |
356 |
{ |
357 |
for ( i = 0; i < document.all(checkboxID).length; i++ ) { |
358 |
if ( document.all(checkboxID).item( i ).checked ) { |
359 |
check += 1; |
360 |
} |
361 |
} |
362 |
} |
363 |
else { |
364 |
if ( document.all(checkboxID).checked ) { |
365 |
check = 1; |
366 |
} |
367 |
} |
368 |
return check; |
369 |
} |
370 |
function getTotalBytes(varField) |
371 |
{ |
372 |
if (varField == null) { |
373 |
return - 1; |
374 |
} |
375 |
var totalCount = 0; |
376 |
for (i = 0; i < varField.value.length; i++) { |
377 |
if (varField.value.charCodeAt(i) > 127) { |
378 |
totalCount += 2; |
379 |
} |
380 |
else { |
381 |
totalCount++ ; |
382 |
} |
383 |
} |
384 |
return totalCount; |
385 |
} |
386 |
function getFirstSelectedValue( checkboxID ) |
387 |
{ |
388 |
var value = null; |
389 |
var i = 0; |
390 |
if ( document.all(checkboxID).length > 0 ) |
391 |
{ |
392 |
for ( i = 0; i < document.all(checkboxID).length; i++ ) |
393 |
{ |
394 |
if ( document.all(checkboxID).item( i ).checked ) { |
395 |
value = document.all(checkboxID).item(i).value; |
396 |
break; |
397 |
} |
398 |
} |
399 |
} |
400 |
else { |
401 |
if ( document.all(checkboxID).checked ) { |
402 |
value = document.all(checkboxID).value; |
403 |
} |
404 |
} |
405 |
return value; |
406 |
} |
407 |
function getFirstSelectedIndex( checkboxID ) |
408 |
{ |
409 |
var value = - 2; |
410 |
var i = 0; |
411 |
if ( document.all(checkboxID).length > 0 ) |
412 |
{ |
413 |
for ( i = 0; i < document.all(checkboxID).length; i++ ) { |
414 |
if ( document.all(checkboxID).item( i ).checked ) { |
415 |
value = i; |
416 |
break; |
417 |
} |
418 |
} |
419 |
} |
420 |
else { |
421 |
if ( document.all(checkboxID).checked ) { |
422 |
value = - 1; |
423 |
} |
424 |
} |
425 |
return value; |
426 |
} |
427 |
function selectAll( checkboxID, status ) |
428 |
{ |
429 |
if ( document.all(checkboxID) == null) { |
430 |
return; |
431 |
} |
432 |
if ( document.all(checkboxID).length > 0 ) |
433 |
{ |
434 |
for ( i = 0; i < document.all(checkboxID).length; i++ ) { |
435 |
document.all(checkboxID).item( i ).checked = status; |
436 |
} |
437 |
} |
438 |
else { |
439 |
document.all(checkboxID).checked = status; |
440 |
} |
441 |
} |
442 |
function selectInverse( checkboxID ) |
443 |
{ |
444 |
if ( document.all(checkboxID) == null) { |
445 |
return; |
446 |
} |
447 |
if ( document.all(checkboxID).length > 0 ) |
448 |
{ |
449 |
for ( i = 0; i < document.all(checkboxID).length; i++ ) |
450 |
{ |
451 |
document.all(checkboxID).item( i ).checked = !document.all(checkboxID).item( i ).checked; |
452 |
} |
453 |
} |
454 |
else { |
455 |
document.all(checkboxID).checked = !document.all(checkboxID).checked; |
456 |
} |
457 |
} |
458 |
function checkDate( value ) |
459 |
{ |
460 |
if (value == '') { |
461 |
return true; |
462 |
} |
463 |
if (value.length != 8 || !isNumber(value)) { |
464 |
return false; |
465 |
} |
466 |
var year = value.substring(0, 4); |
467 |
if (year > "2100" || year < "1900") { |
468 |
return false; |
469 |
} |
470 |
var month = value.substring(4, 6); |
471 |
if (month > "12" || month < "01") { |
472 |
return false; |
473 |
} |
474 |
var day = value.substring(6, 8); |
475 |
if (day > getMaxDay(year, month) || day < "01") { |
476 |
return false; |
477 |
} |
478 |
return true; |
479 |
}; |
480 |
|
481 |
/* |
482 |
用途:检查输入的起止日期是否正确,规则为两个日期的格式正确或都为空且结束日期>=起始日期 |
483 |
输入:startDate:起始日期,字符串; endDate: 结束日期,字符串 |
484 |
返回:如果通过验证返回true,否则返回false |
485 |
*/ |
486 |
function checkPeriod( startDate, endDate ) |
487 |
{ |
488 |
if ( !checkDate(startDate) ) { |
489 |
alert("起始日期不正确!"); |
490 |
return false; |
491 |
} |
492 |
else if ( !checkDate(endDate) ) { |
493 |
alert("终止日期不正确!"); |
494 |
return false; |
495 |
} |
496 |
else if ( startDate > endDate ) { |
497 |
alert("起始日期不能大于终止日期!"); |
498 |
return false; |
499 |
} |
500 |
return true; |
501 |
}; |
502 |
|
503 |
/* |
504 |
用途:检查证券代码是否正确 |
505 |
输入:secCode:证券代码 |
506 |
返回:如果通过验证返回true,否则返回false |
507 |
*/ |
508 |
function checkSecCode( secCode ) |
509 |
{ |
510 |
if ( secCode.length != 6 ) { |
511 |
alert("证券代码长度应该为6位"); |
512 |
return false; |
513 |
} |
514 |
if (!isNumber( secCode ) ) { |
515 |
alert("证券代码只能包含数字"); |
516 |
return false; |
517 |
} |
518 |
return true; |
519 |
}; |
520 |
|
521 |
/* |
522 |
function:cTrim(sInputString,iType) |
523 |
description:字符串去空格的函数 |
524 |
parameters:iType:1=去掉字符串左边的空格;2=去掉字符串左边的空格;0=去掉字符串左边和右边的空格 |
525 |
return value:去掉空格的字符串 |
526 |
*/ |
527 |
function cTrim(sInputString, iType) |
528 |
{ |
529 |
var sTmpStr = ' '; |
530 |
var i = - 1; |
531 |
if (iType == 0 || iType == 1) |
532 |
{ |
533 |
while (sTmpStr == ' ') { |
534 |
++i; |
535 |
sTmpStr = sInputString.substr(i, 1); |
536 |
} |
537 |
sInputString = sInputString.substring(i); |
538 |
} |
539 |
if (iType == 0 || iType == 2) |
540 |
{ |
541 |
sTmpStr = ' '; |
542 |
i = sInputString.length; |
543 |
while (sTmpStr == ' ') { |
544 |
--i; |
545 |
sTmpStr = sInputString.substr(i, 1); |
546 |
} |
sInputString = sInputString.substring(0, i + 1); |
} |
return sInputString; |
}; |

浙公网安备 33010602011771号