工作我想验证名字和姓氏的字段通过JavaScript的HTML文件内。验证必须提示用户是否插入了有效的名称/姓氏,即输入框中不应包含数字或怪异字符。JavaScript的文本验证不使用正则表达式
我一直在寻找计算器和其他类似网站的解决方案,但遗憾的是我没有找到我要找的,所以我希望解决这个网站有人能伸出援助之手的精确解。
这是我的HTML代码:
<form name="contact" method="post" action="contact.php">
<table width="200" border="0" align="center">
<tr>
<td><p class="pagetext">Name:</p></td>
<td><input type="text" name="fname" id="fname" size="30"/></td>
</tr>
<tr>
<td><p class="pagetext">Surname:</p></td>
<td><input type="text" name="sname" id="sname" size="30"/></td>
</tr>
<tr>
<td><p class="pagetext">Email:</p></td>
<td><input type="text" name="email" id="email" size="30"/></td>
</tr>
<tr>
<td><input class="btns" type="submit" onclick="validationMain();validationContact();validationErrors();" value="Submit"/></td>
<td><input class="btns" type="submit" onclick="validationReset()" value="Reset"/></td>
</tr>
</table>
</form>
这是JavaScript代码(留出了不相关的部分,以免混乱的问题):
var errors = "";
var textOnly = /^[A-Za-z]+$/;
function validationMain(){
var firstname = document.getElementById('fname');
var surname = document.getElementById('sname');
var email = document.getElementById('email');
//emailRegEx expression from www.regular-expressions.info/email.html
var emailRegEx = /^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (firstname.value == "") {
errors += "First Name Cannot Be Left Empty\r\n";
} else {
if (firstname.value != textOnly.value){
errors += "First Name is Invalid\r\n";
}
}
if (surname.value == "") {
errors += "Surname Cannot Be Left Empty\r\n";
} else {
if (surname.value != textOnly.value){
errors += "Surname is Invalid\r\n";
}
}
if (email.value != ""){
if (email.value.search(emailRegEx) == -1){
errors += "Invalid Email Entered\r\n";
}
} else {
errors += "Email Cannot Be Left Empty\r\n";
}
}
的主要问题是,无论在文本字段中(即使它们是正确的那些没有数字和特殊字符),它会提示他们是无效的(这不应该是这样),所以我猜有名字或姓氏我输入什么不对的,如果我条件。
预先感谢您的宝贵时间,帮助和提供的任何相关信息:)
我希望我给了足够的信息,如果不是请让我知道!
你用过像[萤火]调试器(http://getfirebug.com/)或[WebKit的检查(https://developers.google.com/chrome-developer-tools/)逐步执行码? – RichardTowers