Reputation: 128
I am doing this project in which I made a registration form in HTML and validated it 'simply' by javascript. By 'simply' I mean that I am just making sure that the input fields are not empty. Now, what's the issue? Everything is working fine until someone select 'others' option from college list (id=cname) and a new input box will appear (which has id=cname1). Now, if someone will leave the input box empty, it would return an alert saying 'College name cant be empty!', but then he is unable to click on the submit button. I double-checked the syntax and everything seems fine. Here is my javascript code:
function validate(){
var name=document.getElementById("name").value;
if(name===""){
document.getElementById("ename").innerHTML="Please Enter a Name";
return false;
}else{
var mail=document.getElementById("mail").value;
if(mail===""){
document.getElementById("email").innerHTML="Please Enter an Email";
return false;
}else{
var num=document.getElementById("num").value;
if(num===""){
document.getElementById("enum").innerHTML="Please Enter Phone number";
return false;
}else{
var batch=document.getElementById("batch").value;
if(batch===""){
document.getElementById("ebatch").innerHTML="Please Enter a Batch";
return false;
}
else{
var cname=document.getElementById("cname").value;
if(cname===""){
document.getElementById("ecname").innerHTML="Please Enter a College Name";
return false;
}else if(cname==="others"){
var cname1=document.getElementById("cname1").value;
if(cname1==="")
alert("College Name can't be empty");
return false;
}else{
var activity=document.getElementById("activity").value;
if(activity===""){
document.getElementById("eactivity").innerHTML="Please Enter a Activity Name";
return false;
}else{
return true;
}
}
}
}
}
}
}
Update 2: The shrink-ed HTML
<tbody>
<tr>
<td>Name</td>
<td><input class="css-input" type="text" id="name" name="name" placeholder="Enter your name"></td>
<td><p id="ename"></p></td>
</tr>
<tr>
<td>Email</td>
<td><input class="css-input" type="text" id="mail" name="mail" placeholder="Enter your Email Id"></td>
<td><p id="email"></p></td>
</tr>
<tr>
<td>Phone</td>
<td><input class="css-input" type="tel" id="num" name="num" Placeholder="Phone Number"></td>
<td><p id="enum"></p></td>
</tr>
<tr>
<td>Course</td>
<td>
<select class="css-input" name="course" id="course">
<option value="mbbs" >MBBS</option>
<option value="bds" selected="selected" >BDS</option>
</select>
</td>
</tr>
<tr>
<td>Batch</td>
<td>
<select class="css-input" name="batch" id="batch" >
<option value="10">2010</option>
<option value="11">2011</option>
<option value="12">2012</option>
<option value="13">2013</option>
<option value="14">2014</option>
<option value="15">2015</option>
</select>
</td>
<td><p id="ebatch"></p></td>
</tr>
<tr>
<td>WorkShop</td>
<td id="first">
<select class="css-input" name="workshop1">
<option value="dentistry">Dentistry</option>
<option value="communicationsoftskill">Communication and Soft Skills</option>
<option value="research">Going about Research</option>
<option value="None">None</option>
</select>
</td><td id="third">
<select class="css-input" name="workshop3">
<option value="communicationsoftskill">Communication and Soft Skills</option>
<option value="research">Going about Research</option>
<option value="None">None</option>
</select>
</td>
<td class="second" id="second">
<select class="css-input" name="workshop2">
<option value="surgery">Surgical</option>
<option value="radiodiagnostic">Radiodiagnostics</option>
<option value="comandsoftskill">communication and soft skills</option>
<option value="emercriticalcare">emergency and critical care</option>
<option value="research">going about research</option>
<option value="None">None</option>
</select>
</td>
</tr>
<tr>
<td>College name</td>
<td>
<select class="css-input" name="cname" id="cname">
<option value="PGIMS, Rohtak">PGIMS, Rohtak</option>
<option value="Swami Devi Dayal Dental College , Panchkula">Swami Devi Dayal Dental College , Panchkula</option>
<option value="BRS Instititute of Medical Sciences Dental College and Hospital , Sultanpur , Panchkula">BRS Instititute of Medical Sciences Dental College and Hospital , Sultanpur , Panchkula</option>
<option value="Kothiwal Dental and Research Centre , Moradabad">Kothiwal Dental and Research Centre , Moradabad</option>
<option value="Teerthankar Mahavir Dental College , Moradabad">Teerthankar Mahavir Dental College , Moradabad</option>
<option value="Teerthankar Mahavir Medical College and Research Center , Moradabad">Teerthankar Mahavir Medical College and Research Center , Moradabad</option>
<option value="ESIC , Faridabad">ESIC , Faridabad</option>
<option value="Maharishi Markandeshwar College of Dental Sciences , Mullana , Ambala">Maharishi Markandeshwar College of Dental Sciences , Mullana , Ambala</option>
<option value="JN Kapoor DAV Centenary Dental College and Hospital , Yamunanagar">JN Kapoor DAV Centenary Dental College and Hospital , Yamunanagar</option>
<option value="Jan Nayak Ch. Devi Lal Dental College , Sirsa">Jan Nayak Ch. Devi Lal Dental College , Sirsa</option>
<option value="Manav Rachna Dental College , Faridabad">Manav Rachna Dental College , Faridabad</option>
<option value="SGT Dental College , Gurgaon">SGT Dental College , Gurgaon</option>
<option value="PDM Dental College and Research Institute , Bahadurgarh">PDM Dental College and Research Institute , Bahadurgarh</option>
<option value="Sudha Rustagi College of Dental Sciences And Research , Faridabad">Sudha Rustagi College of Dental Sciences And Research , Faridabad</option>
<option value="Dr. Harvansh Singh Gudge Institute of Dental Sciences and Hospital , Chandigarh">Dr. Harvansh Singh Gudge Institute of Dental Sciences and Hospital , Chandigarh</option>
<option value="Swami Devi Dayal Hospital and Dental College, Barwala">Swami Devi Dayal Hospital and Dental College, Barwala</option>
<option value="Yamuna Institute of Dental Sciences And Research , Yamunanagar">Yamuna Institute of Dental Sciences And Research , Yamunanagar</option>
<option value="Lala Lajpat Rai Memorial Medical College , Meerut , Uttar Pradesh">Lala Lajpat Rai Memorial Medical College , Meerut , Uttar Pradesh</option>
<option value="Dayanand Medical College , Ludhiana">Dayanand Medical College , Ludhiana</option>
<option value="CMC , Ludhiana">CMC , Ludhiana</option>
<option value="GMC , Chandigarh">GMC , Chandigarh</option>
<option value="AFMC , Pune">AFMC , Pune</option>
<option value="ACMS , Delhi Cantt.">ACMS , Delhi Cantt.</option>
<option value="UCMS , New Delhi">UCMS , New Delhi</option>
<option value="VMMC , New Delhi">VMMC , New Delhi</option>
<option value="Sawai Man Singh Medical College , Jaipur">Sawai Man Singh Medical College , Jaipur</option>
<option value="IGMC , Shimla ">IGMC , Shimla </option>
<option value="Gold Institute of Medical Sciences and Research , Ballabhgarh ">Gold Institute of Medical Sciences and Research , Ballabhgarh </option>
<option value="28. Maharshi Markandeshwar Institute of Medical Sciences and Research , Mullana , Ambala">Maharshi Markandeshwar Institute of Medical Sciences and Research , Mullana , Ambala</option>
<option value="SGT Medical College , Budhera , Gurgaon">SGT Medical College , Budhera , Gurgaon</option>
<option value="Maharaja Agarsen Medical College , Agroha , Hisar">Maharaja Agarsen Medical College , Agroha , Hisar</option>
<option value="Shaheed Hasan Khan Mewati GMC , Mewat ">Shaheed Hasan Khan Mewati GMC , Mewat </option>
<option value="BPS GMC for women , Khanpur">BPS GMC for women , Khanpur32. BPS GMC for women , Khanpur</option>
<option value="GMC , Patiala ">GMC , Patiala </option>
<option value="others">others</option>
</select>
</td>
<td><input class="css-input" type="text" name="cname1" id="cname1" placeholder="enter Your college name"></td>
<input type="hidden" value="1" id="flg" name="flg">
<td><p id="ecname"></p></td>
</tr>
<tr>
<td>Accomodation</td>
<td>
<select class="css-input" id="accom" name="accom">
<option value="yes" selected="selected">YES</option>
<option value="no">NO</option>
</select>
</td>
</tr>
<tr>
<td><input class="css-input" type="reset"></td>
<td><input style="cursor: pointer;" class="css-input" type="submit" ></td>
</tr>
</tbody>
Thanks
Upvotes: 0
Views: 208
Reputation: 6996
The problem is you didn't wrap your if statement in braces.
The offending snippet:
var cname1=document.getElementById("cname1").value;
if(cname1==="")
alert("College Name can't be empty");
return false;
This is the same as writing:
var cname1=document.getElementById("cname1").value;
if(cname1==="") {
alert("College Name can't be empty");
}
return false;
Which means that you will always return false
once you get to this value. This will always prevent the form submission provided the user gets this far.
The fix is straightforward:
var cname1=document.getElementById("cname1").value;
if(cname1==="") {
alert("College Name can't be empty");
return false;
}
For this reason, many consider it a best practice to always include braces when writing JavaScript.
Upvotes: 2