我正在尝试验证我的表单中的几个字段,我不想改变任何东西,因为一切都是相互联系的,我不想弄乱任何东西。基本上我只想验证名称和电子邮件字段,一旦成功,我将转移到其他领域。我尝试使用下面的javascript代码,但它不适合我。
注意:我需要使用 Javascript而不是Jquery ,我需要基本Javascript 而不是复杂,因为我完全新手到Javascript。任何帮助将不胜感激。
var frmvalidator = new Validator("form1");
frmvalidator.addValidation("FirstName","req","Please enter your First Name");
frmvalidator.addValidation("FirstName","maxlen=20",
"Max length for FirstName is 20");
frmvalidator.addValidation("LastName","req");
frmvalidator.addValidation("LastName","maxlen=20");
frmvalidator.addValidation("Email","maxlen=50");
frmvalidator.addValidation("Email","req");
frmvalidator.addValidation("Email","email");
<h1> Contact Form</h1>
<form id="form1" class="campus" method="post">
<div class="form_description">
<h2>User Feedbacks</h2>
<p>Please give us your feedbacks.</p>
</div>
<ul>
<li>
<label class="description" >Name*</label>
<input type="text" name="FullName" id="FirstName" maxlength="50" size="8" />
<label for="FirstName"> First </label>
<input type="text" name= "user" id="LastName" maxlength="50" size="8"/>
<label for="LastName"> Last </label>
</li>
<li >
<label for="Email" class="description">Email*:</label>
<input type="email" name="Email" id="Email" maxlength="55"/>
</li>
<li>
<label class="description" >Reason for Contacting:</label>
<input name="choice" id="Hospitality" type="checkbox" value="1"/>
<label for="Hospitality"> About Website </label>
<input name="choice" id="Technology" type="checkbox" value="1" />
<label for="Technology"> About Deals </label>
<p class="guidelines" ><small>Reason for contacting. Please select all that are applicable</small></p>
</li>
<li>
<label class="description" >Will you Recommend us to anyone.</label>
<select>
<option value="" ></option>
<option value="1" >Yes</option>
<option value="2" >No</option>
<option value="3" >Maybe</option>
</select>
</li>
<li>
<label for="AnyQuestions" class="description" >Any Questions Or Suggessions </label>
<textarea name="AnyQuestions" id="AnyQuestions" class="element textarea large"></textarea>
</li>
<li>
<label class="description" >Do You want to receive special offers. Please Select One</label>
<input type="radio" name="opinion" value="1" />
<label>Yes</label>
<input type="radio" name="opinion" value="2" />
<label> No </label>
</li>
<li class="buttons">
<input type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
答案 0 :(得分:1)
你必须包括&#34; Validator&#34;的定义。 class:
function Validator(frmname, alertMethod) {.....}
这是在代码之上复制的有效Validator类: https://jsfiddle.net/8qsLvx54/1/
代码来自http://www.rockcliffepark.com/rp/CONTACT_validator.js(来自谷歌搜索),您可能想尝试找到此代码的来源......
答案 1 :(得分:1)
JavaScript没有Validator
函数来帮助验证表单。
相反,你可以像jQuery那样构建一个或下载一个,这些额外的代码称为Libraries。
目前浏览器无法理解Validator
是什么,因此会导致错误。但是,如果您包含Parsley或Verify.js之类的内容并根据其文档编写一些代码,则可以归档所需的结果。
答案 2 :(得分:0)
如果不将复杂性隐藏在JavaScript库或框架中,您将无法获得所需的简单(如简短)JavaScript解决方案。但是没有JavaScript解决方案怎么样? Most of what you want can be done with plain HTML5
您已经使用了maxlength
属性,只是将其设置为与您尝试在Javascript中验证的值不同的值。将它们设置为20
以获取名称部分,将50
设置为电子邮件地址,用户甚至无法输入更多字符。
<input type="text" name="FullName" id="FirstName" maxlength="20" size="8"/>
添加required
属性,任何现代浏览器都会突出显示他们是否输入了与type
属性不匹配的内容值。
<input type="text" name="FullName" id="FirstName" maxlength="20" size="8" required/>
对于您已使用type="email"
的电子邮件字段,所以几乎所有设置都只是添加required
。
<input type="email" name="Email" id="Email" maxlength="50" required/>
请注意,构成有效电子邮件地址的内容因浏览器而异。 (请参阅http://www.the-art-of-web.com/html/html5-form-validation/部分&#39; INPUT类型=&#34;电子邮件&#34;&#39; )
要获取一些自定义错误消息you can set the title
attribute:
<input type="text" name="FullName" id="FirstName" maxlength="20" size="8" title="Please enter your First Name (20 characters max.)" required />
(不在StackOverflow片段中工作,不知道原因究竟是什么。)
<h1> Contact Form</h1>
<form id="form1" class="campus" method="post">
<div class="form_description">
<h2>User Feedbacks</h2>
<p>Please give us your feedbacks.</p>
</div>
<ul>
<li>
<label class="description">Name*</label>
<input type="text" name="FullName" id="FirstName" maxlength="20" size="8" title="Please enter your First Name (20 characters max.)" required />
<label for="FirstName">First</label>
<input type="text" name="user" id="LastName" maxlength="20" size="8" required />
<label for="LastName">Last</label>
</li>
<li>
<label for="Email" class="description">Email*:</label>
<input type="email" name="Email" id="Email" maxlength="50" required />
</li>
<li>
<label class="description">Reason for Contacting:</label>
<input name="choice" id="Hospitality" type="checkbox" value="1" />
<label for="Hospitality">About Website</label>
<input name="choice" id="Technology" type="checkbox" value="1" />
<label for="Technology">About Deals</label>
<p class="guidelines"><small>Reason for contacting. Please select all that are applicable</small>
</p>
</li>
<li>
<label class="description">Will you Recommend us to anyone.</label>
<select>
<option value=""></option>
<option value="1">Yes</option>
<option value="2">No</option>
<option value="3">Maybe</option>
</select>
</li>
<li>
<label for="AnyQuestions" class="description">Any Questions Or Suggessions</label>
<textarea name="AnyQuestions" id="AnyQuestions" class="element textarea large"></textarea>
</li>
<li>
<label class="description">Do You want to receive special offers. Please Select One</label>
<input type="radio" name="opinion" value="1" />
<label>Yes</label>
<input type="radio" name="opinion" value="2" />
<label>No</label>
</li>
<li class="buttons">
<input type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
&#13;