亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js 表單驗(yàn)證方法(實(shí)用)

 更新時(shí)間:2009年04月28日 19:10:27   作者:  
每次都要用到表單驗(yàn)證的時(shí)候,就在到處找東西,而網(wǎng)上的很多都不見(jiàn)的好用。自己終于累計(jì)了這些方法,在自己的程序中使用了的一些方法,肯定是好用的,主要使用了簡(jiǎn)單的正則表達(dá)式進(jìn)行判斷。如果有bug,歡迎提出來(lái)。
//下面驗(yàn)證的是長(zhǎng)度
function checkTextLen(textId){
var len = 0;
var checkField=document.getElementById(textId);
var inputstring = checkField.value;
var string_length = inputstring.length;
if (string_length == 0)
{
return 0;
}
for (var i=0;i<string_length;i++)
{
if (inputstring.charAt(i).charCodeAt()>255) len+=2;
else len+=1;
}
return len;
}
function checkTextLength(textId,length,msg){
var textObj =document.getElementById(textId);
if(checkTextLen(textId)>length/1){
alert("["+msg+"]"+"長(zhǎng)度最大為"+length+"位,"+"請(qǐng)重新輸入!注意:一個(gè)漢字占2位");
textObj.focus();
return false;
}else {
return true;
}
}
//下面驗(yàn)證不含有非法的字符,中文,英文,數(shù)字都是合法的。
function isValidString(textId,errMsg){
szStr = document.getElementById(textId).value;
voidChar = "'\"><`~!@#$%^&\(\)()?。ぁ?“”‘'*";
for(i = 0 ; i < voidChar.length; i ++){
aChar = voidChar.substring(i, i + 1);
if(szStr.indexOf(aChar) > -1){
alert(errMsg);
return false;
}
}
return true;
}
//下面驗(yàn)證只可以輸入字母,數(shù)字,下劃線(xiàn)
function isEnglish(textId,errMsg)
{
s = document.getElementById(textId).value;
//下面的正則表達(dá)式限制的長(zhǎng)度在6到20之間
//var patrn=/^(\w){6,20}$/;
var patrn =/^(\w)*$/;
if (!patrn.exec(s)){
alert(errMsg);
return false
}
return true
}
//下面驗(yàn)證只允許中文
function isChinese(textId,errMsg)
{
s = document.getElementById(textId).value;
var patrn =/[^\u4E00-\u9FA5]/g;
if (patrn.exec(s)){
alert(errMsg);
return false
}
return true
}
//下面驗(yàn)證只允許數(shù)字
function isNumber(textId,errMsg)
{
s = document.getElementById(textId).value;
//下面的正則表達(dá)式限制的長(zhǎng)度在6到20之間
//var patrn=/^(\d){6,20}$/;
var patrn =/^(\d)*$/;
if (!patrn.exec(s)){
alert(errMsg);
return false
}
return true
}
使用js的正則表達(dá)式用來(lái)控制不允許在文本框里面輸入非數(shù)字,也就是只允許輸入數(shù)字。調(diào)用方法 : onkeyup="onlyNum(this);"
function onlyNum(obj)
{
temp = obj.value;
//注意下面的正則表達(dá)式的寫(xiě)法,沒(méi)有用引號(hào)括起來(lái)。。
obj.value = temp.replace(/\D/g,'');
}

相關(guān)文章

最新評(píng)論