function validateForm()
{
var x=document.forms["pageFeedbackSurvey"]["emailAddr"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Please enter a valid e-mail address");
return false;
}
var x=document.forms["pageFeedbackSurvey"]["comments"].value;
if (x==null || x=="")
{
alert("Please enter your feedback into the textbox before pressing 'Submit'");
return false;
}
var phone = document.getElementById("phone").value;
var pattern = /^([0-9]){7,15}$/;
if (pattern.test(phone))
{
alert("Your phone number : "+phone);
return true;
}
alert("It is not valid phone number!");
return false;
}
然后是html
<form action="demo_form.asp" onsubmit="return validateForm()" method="post" name="pageFeedbackSurvey" id="pageFeedbackSurvey">
<div class="pod pageFeedback">
<h2>Feedback</h2>
<div class="body">
<fieldset>
<div class="surveyrow">
<p id="q1Wording">Did you find the information you were looking for on this page?</p>
<ul>
<li> <!-- input uses class, name and value for jquery -->
<input name="showdiv2" id="survey1_1" value="Yes" type="radio" class="showsubmit"/><label for="survey1_1">Yes</label>
</li>
<li>
<input name="showdiv" id="survey1_2" value="No" type="radio" class="showform"/><label for="survey1_2">Partially</label>
</li>
<li>
<input name="showdiv" id="survey1_3" value="No" type="radio" class="showform"/><label for="survey1_3">No</label>
</li>
</ul>
</div>
<div id="extraFields">
<div class="commentRow">
<div id="hiddentextarea"> <!-- hidden div -->
<label for="comments">How can we improve this page?</label>
<textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,300);"
onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,300);" id="comments" rows="8" cols="19"></textarea>
<p>Character limit: <input readonly="readonly" type="text" name="countdown" id="countdown" size="3" value="300"></input></p>
</div>
</div> <!-- end hidden div -->
<div id="hiddenfields"> <!-- hidden div -->
<div class="emailContainer">
<div class="email" id="email">
<input type="text" name="emailAddr" id="emailAddr" size="22" value="Enter email address"/>
</div>
</div>
<div class="phoneContainer">
<div class="phone" id="phone">
<input type="text" name="phone" id="phone" type="phone" size="22" value="" />
</div>
</div>
<div id="pageFeedbackSubmit" class="button primary right">
<input class="arrow" onclick="validate();" value="Submit" type="submit" id="surveySubmit"/>
</div><!-- button -->
</div> <!-- end hidden div -->
<div class="clearBoth"></div>
</div>
</fieldset>
<div id="errormsg" class="errorMessage" style="display: none"></div>
<div id="thankYou" style="display:none">
<h3>Thank you</h3>
<p>We appreciate you taking the time to help us.</p>
</div>
我一直在努力寻找在类型或提交时验证电话输入的解决方案。在选择单选按钮之前,我一直隐藏表单,按下单选按钮后显示电子邮件和电话以及文本区域,然后我让它验证文本区域的输入和电子邮件的语法,现在我需要验证电话为一个英国号码,我设法让电话字段至少显示一个不正确的弹出警报,但我无法让它接受当前设置中的任何号码。
有人可以建议我在提交时执行 validateForm 功能的同时验证手机以及电子邮件和文本区域的正确方法吗?
以下所有内容添加/编辑 23/08/2012
嘿伙计们感谢所有的回复,但我仍然无法让它工作,我已经更改了 id,所以phone
不再有 2 个 id,我将 showdiv2 更改为 showdiv,我在插入 javascript telnumber 检查之前将输入类型更改为 tel
我将 braemoor.co.uk/software/telnumbers.shtml 中的脚本添加到标题中的 javascript 文件中,然后尝试将该 javascript 绑定到 html,该示例中没有 html,所以我一直在猜测...
已为电子邮件和文本区域设置的验证功能由
<form action="demo_form.asp" onsubmit="return validateForm()"
这很好用,我想要的是电话功能也可以在提交时或之前运行?我尝试将其添加到提交按钮
<input class="arrow" onclick="checkUKTelephone();" value="Submit" type="submit" id="surveySubmit"/>
但它不会验证手机,它只是在验证电子邮件和文本区域后忽略电话号码,然后转发到演示页面,显然我没有做正确的事情,我没有大量的 JavaScript 经验,你可能已经注意到了:)
有人好心告诉我我哪里出错了吗?