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>

Your current code is almost correct, but there are a few issues that need to be fixed in order to correctly validate all the required fields. Here's an updated version of your code:

```html
<!DOCTYPE html>
<html>
<head>
<script>
function validate() {
var name = document.getElementById("n").value;
var email = document.getElementById("e").value;
var nic = document.getElementById("nu").value;

if (name === "") {
alert("Name cannot be empty");
return false;
}

if (email === "") {
alert("Email cannot be empty");
return false;
}

if (nic.length !== 10) {
alert("Please enter a correct NIC number");
return false;
}

alert("Successful entry");
return true;
}
</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>
<tr>
<td align="left"><input type="submit" onclick="return validate()" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
```

I made the following changes to your code:

1. Added `return false` statements for each validation rule to prevent the form from being submitted if any of the validations fail.

2. Fixed the closing tags for the `<tr>` elements.

3. Added double equals (`==`) for comparisons in the if conditions.

Now, the form should validate all the required fields correctly and display the appropriate error messages. If all validations pass, it will display the "Successful entry" message.