Question

Question :



Create the form structure shown below and write a JavaScript function satisfy the following

requirements.

 The Name and the email address are required fields while entering data.

 Number of characters of the national identity card number should be equal to 10.

 If above rules are violated by the user, relevant error messages should be displayed

 Otherwise the message “Successful Entry” should be displayed.



I wrote the following code, but it doesn't validate all required fields.



<DOCTYPE! html>

<html>

<head>



<script>

function validate() {



var a = document.getElementById("n").value;

var b = document.getElementById("e").value;

var c = (document.getElementById("nu").value).length;



if(a==""){



alert("Name cannot be empty");



if(b==""){



alert("Email cannot be empty");



if(c!=10){



alert("Please enter a correct NIC number");



}



else{



alert("Successful entry");



}



}

}



}



</script>



</head>



<body>



<form method="post">

<table>



<tr>

<td align="right"> Name </td>

<td><input type="text" id="n" /> </td>

<tr>



<tr>

<td align="right"> Email </td>

<td><input type="text" id="e" /> </td>

<tr>



<tr>

<td align="left"> NIC Number </td>

<td> <input type="text" id="nu" size=10 maxlength=10 /> </td>

<tr>



<td align="left"> <input type="submit" onclick="validate()" name="submit" value="Submit"/></td>



</tr>



</table>

</form>



</body>



</html>

Answers

Related Questions