我需要验证订阅者名称,我只需要查找字符串是否包含任何数字并提示错误消息,如果不是,则用户应该收到消息“您的联系信息有效”。现在通过创建简单的功能,但它不起作用,为什么呢?
html代码:
<input id="name" placeholder="Enter your name" type="text" required>
<input id="email" placeholder="Enter your email" type="email" required>
<button type="submit" id="submitButton">SUBMIT</button>
js code:
var name = document.getElementById('name');
var email = document.getElementById('email');
var submitButton = document.getElementById('submitButton');
submitButton.onclick = function(){
if(email.value.indexOf("@") < 1 || email.value.lastIndexOf(".") - email.value.indexOf("@") < 2)
if(hasNumbers(name.value))
alert("Please enter valid contact info");
return false;
函数hasNumbers(t) 返回/\d/.test(t);
答案 0 :(得分:0)
submitButton.onclick = function(){
var name = document.getElementById('name').value;
if (!/^[a-zA-Z]*$/g.test(name))
alert("Please enter valid contact info");
return false;
}
请尝试这样