Ashley P.

asked • 11/17/19

JavaScript Form Validation

Question :

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


 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>




function validate() {

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

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

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


alert("Name cannot be empty");


alert("Email cannot be empty");


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



alert("Successful entry");








<form method="post">



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

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



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

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



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

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


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






1 Expert Answer


Patrick B. answered • 11/17/19

Math and computer tutor/teacher

Ashley P.

That works!!!! Thank you very much!


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.


Find an Online Tutor Now

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