Ashley P.

asked • 11/17/19

JavaScript Form Validation

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>

1 Expert Answer

By:

Patrick B. answered • 11/17/19

Math and computer tutor/teacher

Ashley P.

That works!!!! Thank you very much!
Report

11/17/19

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.