function ValiKey( obj )
{
//字符输入验证
if ( ( window.event.keyCode < 97 || window.event.keyCode > 122 ) &&
( window.event.keyCode < 48 || window.event.keyCode > 57 ) &&
( window.event.keyCode < 65 || window.event.keyCode > 90 ) )
{
window.alert( "Please input A-Z or a-z or 0-9 chars!" );
window.event.returnValue = false;
obj.focus();
}
}
function ValiEmail( obj )
{
//邮件地址验证
var strEmail = new String( obj.value );
if ( ( strEmail.indexOf("@",0) == -1 || strEmail.indexOf(".",0) == -1 || strEmail.length < 5 ) )
{
window.alert( "E-mail address error!" );
obj.focus();
obj.select();
}
}
===============
调用:
<input id="username" name="username" type="text" onkeypress="ValiKey(this);"/>
<input id="password" name="password" type="password" onchange="ValiEmail(this);"/>
[乱七八糟]验证JS
最新推荐文章于 2024-09-11 20:25:47 发布
博客给出了两个 JavaScript 函数,ValiKey 用于字符输入验证,限制输入为 A - Z、a - z 或 0 - 9;ValiEmail 用于邮件地址验证,检查邮件格式。还展示了如何在 input 标签中调用这两个函数进行输入验证。
964

被折叠的 条评论
为什么被折叠?



