-5

我不知道java,但这对于知道的人来说看起来很容易。我需要验证联系表单,并且我有这些验证脚本,但如果我输入此脚本的多个实例,它们不会运行,如果只使用其中一个实例,它们就可以正常工作。有人可以将这 4 个组合成一个验证脚本吗?

验证名称字段

<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["name"].value;
if (x==null || x=="")
  {
  alert("Name must be filled out");
  return false;
  }
}
</script>

验证主题字段

<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["subject"].value;
if (x==null || x=="")
  {
  alert("Subject must be filled out");
  return false;
  }
}
</script>

验证消息字段

<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["message"].value;
if (x==null || x=="")
  {
  alert("Message must be filled out");
  return false;
  }
}
</script>

验证电子邮件字段

<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
}
</script>

以及您的典型联系表格

<form name="myForm" method="POST" onsubmit="return validateForm()" >
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="subject">
<textarea name="message"></textarea>
<input type="submit" value="Send Message" name="submit">
</form>
4

3 回答 3

2

This is how I got it work

<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["name"].value;if (x==null || x==""){alert("Name must be filled out");return false}
var x=document.forms["myForm"]["email"].value;var atpos=x.indexOf("@");var dotpos=x.lastIndexOf(".");if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){alert("Not a valid e-mail address");return false;}
var x=document.forms["myForm"]["subject"].value;if (x==null || x==""){alert("Subject must be filled out");return false}
var x=document.forms["myForm"]["message"].value;if (x==null || x==""){alert("Message must be filled out");return false}
}
</script>
于 2012-07-24T08:33:50.960 回答
1

Something like;

var validation = ["name","subject","message","email"];   
validation.forEach(
    x=document.forms["myForm"][this];
    if(this=="email"){
        var atpos=x.indexOf("@");
        var dotpos=x.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
             alert("Not a valid e-mail address");
             return false;
        }
   }else{
        if (this==null || x==""){
            alert(this.toUpperCase()." must be filled out");
            return false;
        }
    }
);
于 2012-07-23T22:11:27.193 回答
0

您可以使用条件“if”使其在一个函数中工作,使用表单中的提交事件调用该函数,我认为它比您想象的最简单和容易,这是一个简单表单的示例:

<form action="insert.php" name="contact" onsubmit="return valida_frm(this)" method="post">

               <input name="personname" type="text"/>
               <input name="personemail" type="email"/>                  
               <textarea name="message"></textarea>
               <input type="submit" value="Send">

            </form>

然后在“head”标签之间添加函数

<script language="javascript">
function valida_frm(form){
var alert="Error:\n";
if (form.personname.value == "") {alert+="Name can't be empty.\n";} 
if (form.personemail.value == "") {alert+="E-Mail can't be empty.\n";}
if (form.message.value == "") {alert+="Message can't be empty.\n";}
if (alert!="Error:\n"){
    alert(alert);   
    return false;
}else{
    return true;
}   
}
</script>

您可以使用这种方式来验证输入值并将其发送到您的邮件程序。我希望这对你有帮助。

于 2013-04-30T02:11:41.877 回答