2014-01-19 52 views
0

我已經在html中創建了一個表單,我必須使用JavaScript驗證它.First Name和Father的名字不應該超過20個字符,所以我已經爲它們放置了檢查,但是他們在工作。這是我的代碼在JavaScript中的表單驗證(事件處理)

<html> 
    <head> 
     <script type="text/javascript"> 
     function validate() 
     { 
      var firstName=document.f1.firname.value; 
      var fatherName=document.f1.fname.value; 
      var address=document.f1.add.value; 
      var phoneNumber=document.f1.ph.value; 
      var cnic=document.f1.cnic.value; 
      var email=document.f1.email.value; 
      var cgpa=document.f1.fname.value; 
      var sem=document.f1.sem.value; 
      var id=document.f1.cid.value; 
      if(firstName.length>20) 
      { 
       alert("Value can't exceed 20"); 
      } 
      if(fatherName.length>20) 
      { 
       alert("Value can't exceed 20"); 
      } 
     } 
     </script> 
    </head> 
    <body> 
    <form name="f1"> 
     Name : <input type="text",name="firname"> <br> 
     Father's Name: <input type="text",name="fname"> <br> 
     Address: <input type="text",name="add"> <br> 
     Phone No.:<input type="text",name="ph"> <br> 
     CNIC:<input type="text",name="cnic"> <br> 
     Email:<input type="text",name="email"> <br> 
     City :<br> <input type="radio" name="city" value="lhr"> Lahore <br> 
     <input type="radio" name="city" value="karachi"> Karachi <br> 
     <input type="radio" name="city" value="isl"> Islamabad <br> 
     <select name="country"> 
      <option value="pakistan">Pakistan</option> 
      <option value="india">India</option> 
      <option value="china">China</option> 
     </select> <br> 
     Cgpa:<input type="text",name="cgpa"> <br> 
     Department:<input type="text",name="dpt"> <br> 
     <select name="degree"> 
      <option value="se">SE</option> 
      <option value="cs">CS</option> 
      <option value="it">IT</option> 
     </select> <br> 
     Semester:<input type="text",name="sem"> <br> 
     CollegeId:<input type="text",name="cid"> <br> 
     <input type="Submit", value="Submit" ,onsubmit="validate()"> 
    </form> 
    </body> 

什麼似乎是問題?

+0

不應該有分離在HTML屬性,這''應'<輸入類型的任何逗號= 「提交」值=「提交」onsubmit =「驗證()」> – MichaelJames

+0

警報彈出仍然沒有出現.. – Ahmed

回答

1

請勿使用逗號分隔HTML屬性。它應該是:

Name : <input type="text" name="firname"> <br> 

,而不是:

Name : <input type="text",name="firname"> <br> 

onsubmit事件應該是形式,而不是input按鈕。

<form name="f1" onsubmit="validate()"> 
+0

謝謝,這對我工作... – Ahmed