Javascript <script> function keypress(e)
{ if ([e.keyCode||e.which]==8) //this is to allow backspace returntrue; if ([e.keyCode||e.which] <48|| [e.keyCode||e.which] >57)
e.preventDefault? e.preventDefault() : e.returnValue =false;
} </script> <input type="text" name="textbox1" id="textbox1" onKeypress='keypress(event)'>
EMail判断
Javascript <script language=javascript> function isEmailFormatValid(emailSrc) { var email = emailSrc.replace(/^\s+|\s+$/g, ''); if (email =='') { returnfalse;
}
var regex =/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; if (regex.test(email)) { var str =" !#$%^&*()+=|\{[]}:;'<,>?/" ; var str2 ='"'; var result =true; for (var j =0; j < email.length; j++) { if (str.indexOf(email.charAt(j)) !=-1|| str2.indexOf(email.charAt(j)) !=-1) {
result =false; break;
}
} if (result ==true) { returntrue;
} else { returnfalse;
}
} else { returnfalse;
}
}
function btn2_Click() { var strTest; if (isEmailFormatValid(document.all['TextBox7'].value) ==true)
{
strTest ='It is ok'
} else {
strTest ='It is failed'
}
alert(strTest);
} </script>
半角判断
Javascript <script language=javascript> function checkNarrow(character) { for (var i =0; i < character.length; i++) { if (escape(character.charAt(i)).length >=4 ) { returnfalse;
}
} returntrue;
}
function btn1_Click() { var strTest; if (checkNarrow(document.all['TextBox6'].value) ==true)
{
strTest ='It is ok'
} else {
strTest ='It is failed'
}
alert(strTest);
} </script>
获得字节数
Javascript <script language=javascript> function getLengthByte(varField)
{ var i=0; var j=0; for (var i =0; i < varField.length; i++) { if ((varField.charCodeAt(i) >0&& varField.charCodeAt(i) <127 ) || (varField.charCodeAt(i) >65376&& varField.charCodeAt(i) <65440)){
j=j+1;
} else {
j=j+2
}
} return j;
} </script>