測試字串是否符合正規表示:
/**
* Check string
* @date 2012/08/13
* @by Ron Lee
*/
//letters and numbers and underscore
function validLiteral(INPUT) {
var pattern = /^(\w+)$/; // a-zA-Z0-9_
var result=pattern.test(INPUT);
return result;
}
//numbers
function validNum(INPUT) {
var pattern = /^(\d+)$/; // 0-9
var result = pattern.test(INPUT);
return result;
}
//account
function validAccount(INPUT) {
var pattern = /^[a-zA-Z]+([-_\.]\w+)*$/;
var result=pattern.test(INPUT);
return result;
}
//password
function validPassword(INPUT) {
var pattern = /^([^\'|\"]+)$/; // quote is not allowed
var result=pattern.test(INPUT);
return result;
}
function validEmail(INPUT) {
var pattern = /^(\w+)([-\.\w]*)@([-\w]+)(\.[-\w]+)+$/i;
var result = pattern.test(INPUT);
return result;
}
//phone number
function validPhone(INPUT) {
var numExp = /([\d-]+)/; // phone number: numbers and hyphens: 12-3456-789
var result = pattern.test(INPUT);
return result;
}
//date
function validDate(year, month, day) {
var dateStr = (year+'/'+month+'/'+day).replace(/\b(\d)\b/g, '0$1');
if (!dateStr) return false;
if (!dateStr.match(/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/)) return false;
var dateObj = new Date(dateStr);
var get_dateStr = dateObj.getFullYear()+'/'+(dateObj.getMonth()+1)+'/'+dateObj.getDate();
get_dateStr = get_dateStr.replace(/\b(\d)\b/g, '0$1');
//document.write(dateStr+'<BR>'+get_dateStr+'<BR>');
return dateStr==get_dateStr;
}
function addTimeZero(dateString){
return document.write(dateString.replace(/\b(\d)\b/g, '0$1'));
}
//file name in url
function getUrlFilename(INPUT) {
var url = window.location.toString();
var result= url.split(/(\\|\/)/g).pop();
return result;
}
沒有留言:
張貼留言