0
对不起,如果问题是非常基本的 - 我来自web东西(HTML/CSS)的设计方面,而且是JavaScript的新手。我的前两个验证正在工作(检查名称和电子邮件的空字段),但我无法获得显示非法字符检查的错误消息。 这里是代码,我从各种'how to'来源将它们拼凑在一起。任何帮助将不胜感激!检查Javascript无效的无效字符
function validateForm(){
var noName=document.forms["myForm"]["sender_name"].value;
noEmail=document.forms["myForm"]["sender_email"].value;
illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
if (noName==null || noName==""){
alert("Bitte geben Sie Ihren Namen ein");
return false;
}
if (noEmail==null || noEmail==""){
alert("Bitte geben Sie Ihre Email-Adresse ein");
return false;
}
if (noName.match(illegalChars)){
alert("Ungültige Eingabe");
return false;
}
}
工作对我来说:HTTP
而且当你在你需要记住使用var语句每个变量或使用声明之间一个var和逗号顶部声明变量://jsfiddle.net/barmar/7L3CQ/2/ – Barmar
您不需要在正则表达式中跳过'[]'中的大多数字符。你最后一个'if'中还有一些不必要的额外的大括号,并且你错过了该函数的大括号。 – Barmar
谢谢巴尔玛。它是一个php文档的一部分,它现在刚刚上传到服务器,但在本地测试时,错误消息没有与其他JS消息一起显示。感谢您指出语法错误,我现在已经纠正它们 - 周末愉快! –