需要知道表单的JavaScript验证是否正确

时间:2011-12-12 22:55:03

标签: javascript html asp-classic

我在我的叔叔网站上有一个在asp页面中创建的表单。 asp页面提交到php页面。这是我可以解决FastHost实际上让电子邮件通过邮件的唯一方法。

我正在尝试在表单的head部分添加验证,但这似乎不起作用。

头部和表格中的代码如下:

JavaScript的:

<script type="text/javascript">

function validateForms() {
  var x=document.forms["myForm"]["frmname"].value;
  if (x==null || x=="") {
    alert("First name must be filled out");
    return false;
 }

  var x=document.forms["myForm"]["frmemail"].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 action="sendmail.php" onsubmit="return validateForms();" id="myForm" method="post">
                    <table  border="0" cellpadding="2" cellspacing="0" class="text">
                        <tr>
                            <td><p>Name : * </p></td>
                            <td><input name="frmName" id="frmName" type="text" class="textbox" value="<%=frmName%>" size="35" title="Required" /></td>
                        </tr>
                        <tr>
                            <td><p>Email : *</p></td>
                            <td><input name="frmEmail" id="frmEmail" type="email" class="textbox" value="<%=frmEmail%>" size="35" title="Required" /></td>
                        </tr>
                        <tr>
                            <td><p>Phone : </p></td>
                            <td><input name="frmPhone" id="frmPhone" type="text" class="textbox" value="<%=frmPhone%>" size="35" /></td>
                        </tr>
                        <tr>
                            <td><p>Address :</p></td>
                            <td><input name="frmAddress" id="frmAddress" type="text" class="textbox" value="<%=frmAddress%>" size="35" /></td>
                        </tr>
                        <tr>
                            <td><p>How did you hear about us :</p></td>
                            <td><select name="frmReferral" id="frmReferral" class="block" style="width: 180px">
                            <option value=""> - SELECT - </option>
                            <option value="Magazine advert"<%if frmReferral = "Magazine advert" then%> selected="true"<%end if%>>Magazine advert</option>
                            <option value="Booklet advert"<%if frmReferral = "Booklet advert" then%> selected="true"<%end if%>>Booklet advert</option>
                            <option value="Web search"<%if frmReferral = "Web search" then%> selected="true"<%end if%>>Web search</option>
                            <option value="Web directory"<%if frmReferral = "Web directory" then%> selected="true"<%end if%>>Web directory</option>
                            <option value="Newspaper"<%if frmReferral = "Newspaper" then%> selected="true"<%end if%>>Newspaper</option>
                            <option value="TV"<%if frmReferral = "TV" then%> selected="true"<%end if%>>TV</option>
                            <option value="Other"<%if frmReferral = "Other" then%> selected="true"<%end if%>>Other</option>
                            </select>
                            </td>
                        </tr>
                        <tr>
                            <td><p>Do you require a call back :</p></td>
                            <td><select name="frmCallback" id="frmCallback" class="block" style="width: 180px">
                            <option value=""> - SELECT - </option>
                            <option value="Morning"<%if frmCallback = "Morning" then%> selected="true"<%end if%>>Morning</option>
                            <option value="Afternoon"<%if frmCallback = "Afternoon" then%> selected="true"<%end if%>>Afternoon</option>
                            <option value="Not Required"<%if frmCallback = "Not Required" then%> selected="true"<%end if%>>Not Required</option>
                            </select>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" valign="top"><p>Enquiry :</p></td>
                            <td><textarea name="frmEnquiry" id="frmEnquiry" cols="27" rows="4" class="textbox" style="width: 180px"><%=frmEnquiry%></textarea></td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td align="right"><input name="submit" type="submit" value=" Send " class="submit">
                            &nbsp;&nbsp;&nbsp;
                            <input name="reset" type="reset" class="submit"></td>
                        </tr>
                    </table>
                    <p>&nbsp;</p>
                </form>

我将不胜感激任何帮助。感谢

1 个答案:

答案 0 :(得分:0)

frmname和frmemail需要像输入标签一样使用驼峰。

var x=document.forms["myForm"]["frmName"].value;
var x=document.forms["myForm"]["frmEmail"].value;