2013-10-09 61 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