2013-10-09 64 views
1

有些人在下面的表格中帮助我在下面的表格中检查数字/数字是否应该在fname和lname中存在, stnumber shoud有特定的大小(大小) 状态和pincode应该匹配 是否有可能与JavaScript验证?????对HTML表单的验证

<form id="user" method="post" action="" onsubmit="return validateForm()"> 
<span>First Name:</span>      
    <input type="text" class="input" id="fname" name="fname" required> 
    <span>Last Name</span> 
<input type="text" class="input" id="lname" name="lname" required> 
    <span>Student Number</span> 
<input type="text" class="input" id="stn" name="stn" required> 
<span>Student address</span> 
<input type="text" class="input" id="stad" name="stad" required> 
<span>Town/suburb</span> 
<input type="text" class="input" id="town" name="town" required> 
<span>State</span> 
<select name="state" id="state"> 
<option selected>--select--</option> 
<option>AAA</option> 
<option>BBB</option> 
    </select>        
<span>Postal Code</span> 
<input type="text" class="input" id="pcode" name="pcode" required> 

+0

是的。你的JavaScript是什么样的? – putvande

+0

像这样的东西 – fab

+0

你的意思是由javacript验证器? –

回答

-2

如果您的项目allowes你,你可以使用特定的HTML5属性,如需要,最小值,最大值和模式去HTML5验证。查看W3Schools.comhtml5pattern.com

更多信息如果JavaScript是一种需求,有验证库的充足的选择在那里如完全有能力做你想要达到什么样的jQuery ValidateParsley

+0

为你提供帮助:) – fab

+1

[请不要链接到w3schools。这是一个可怕的参考充满不正确的信息,有时甚至会导致安全漏洞。](http://w3fools.com) – ThiefMaster

0

我真的不明白的问题,但如果你希望用户输入一个“特定”的大小,你为什么不只是使用

maxlength="size" 

财产?

编辑: JavaScript部分

function isCharKey(evt){ 
     var charCode = (evt.which) ? evt.which : event.keyCode 
     return !(charCode < 31 && (charCode > 48 || charCode < 57)); 
} 
var state= document.getElementById("state").value; 
var pcode= document.getElementById("pcode").value; 
if (state != pcode) { 
    alert("State and Pcode doesn't match"); 
} 

在HTML

<input type="text" class="input" id="fname" name="fname" required onkeypress="return isCharKey(event);"> 
<input type="text" class="input" id="lname" name="lname" required onkeypress="return isCharKey(event);"> 
<input type="text" class="input" id="stn" name="stn" required maxlength="10"> 

应该这样做对于只输入字符,而不是数字

编辑2:

<script type="text/javascript"> 
function isCharKey(evt){ 
    var charCode = (evt.which) ? evt.which : event.keyCode 
    return !(charCode < 31 && (charCode > 48 || charCode < 57)); 
} 
function validateForm() { 
    var state= document.getElementById("state").value; 
    var pcode= document.getElementById("pcode").value; 
    if (state != pcode) { 
     alert("State and Pcode doesn't match"); 
    } 
}</script> 

在页面底部写这个

+0

srry,文本区域电话我只需要10个数字以上它应该显示警报.... – fab

+0

和在正常的fname和lname它不应该允许numiricals我需要条件fa tht可以üplz帮助我thnq :) – fab

+0

有没有像电话领域?你需要发布更多的代码 – FreshPro