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功能进行操作的同时将电话与电子邮件和文本区域一起验证的正确方法吗?
phone
id,我将showdiv2更改为showdiv,我将输入类型更改为tel之前我插入了javascript telnumber check
我将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的经验,因为你可能已经注意到:)
任何人都可以告诉我哪里出错了?
答案 0 :(得分:0)
HTML中存在问题:
<div class="phoneContainer">
<div class="phone" id="phone">
<input type="text" name="phone" id="phone" type="phone" size="22" value="" />
</div>
</div>
如您所见,DIV和INPUT的id“phone”有两次。
因此,当您尝试获取要验证的电话号码时,它会返回“未定义”,这就是您验证实际电话号码的真实情况。
<div class="phoneContainer">
<div class="phone" id="phoneDiv"> <!-- change the name here -->
<input type="text" name="phone" id="phone" type="phone" size="22" value="" />
</div>
</div>
在HTML文档中,两个Elements可以具有相同的名称,但id应为uniq。
有关基于代码的完整示例,请查看:https://docs.google.com/open?id=0B31oh4Omo6z0YjFmNHc2bjJCMk0