我有一个表格
<form id="login_form" action="login.aspx" method="post" onSubmit="return isNumeric();">
<label for="nid"> Number:</label>
<input type="hidden" name="Context" value="NonAdminLogin" />
<input type="text" name="nid" id="numbers" maxlength="25" tabindex="1" />
<input type="submit" class="button" value="Log In" tabindex="3" />
</form>
在提交之前,我想确保输入 name="nid"不包含所有数字。
例如,如果它有“9909k”,那就很好,但如果它有“45645”,那么我想显示 表格下方的消息。
我已经尝试过这个功能
function isNumeric(){
var numexp = /^[0-9]+$/;
if(document.getElementById('numbers').value.match(numexp)){
return true;
}else{
alert("Entry must be a number");
elem.focus();
return false;
}
}
验证它是否是一个数字,但不起作用。
最佳答案
你的 fiddle 可以工作,但你需要从 onLoad
更改脚本至No wrap - in <head>
.
并且您使用了未声明的变量。
elem.focus();
正如@imtheman 在下面的 fiddle 中所做的那样。更改elem.focus();
到。
document.getElementById('numbers').focus();
关于javascript - 在提交表单并显示消息之前,如何检查表单输入是否不全是数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28312016/