- 2074 Asoj · 10 marks
HTML registration form with JS validation
Short answer
Validation uses onsubmit returning false to cancel. It checks all fields compulsory, name letters only, phone digits only, email format, and interest length 20-1000 characters.
Design a HTML User Registration form as below. [Figure: "User Registration Form" with fields Name (text box), Email (text box), Gender (radio buttons Male / Female), Phone (text box), Interest (large text area) and a Register button]
Validate the registration form using JavaScript. Validation should be as: i. All fields are compulsory. ii. Name should be all characters iii. Phone should be all numbers iv. Validate email format v. Interest should be in between 20 to 1000 characters.
Assumptions: the form is named regForm; Name may contain only letters (and spaces); Phone may contain only digits; Interest must contain 20 to 1000 characters.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Registration</title>
<style>
body { font-family: Arial; }
table { margin: 20px auto; border: 1px solid #666; padding: 10px; }
td { padding: 6px; }
.err { color: red; font-size: 13px; }
</style>
<script>
function validateForm() {
var f = document.forms["regForm"];
var name = f.name.value.trim();
var email = f.email.value.trim();
var phone = f.phone.value.trim();
var interest = f.interest.value.trim();
var gender = f.gender.value; // "" if none selected
var msg = "";
// i. all fields compulsory
if (name == "" || email == "" || phone == "" || gender == "" || interest == "") {
alert("All fields are compulsory.");
return false;
}
// ii. name: only characters
if (!/^[A-Za-z ]+$/.test(name)) {
msg += "Name must contain only letters.\n";
}
// iii. phone: only numbers
if (!/^[0-9]+$/.test(phone)) {
msg += "Phone must contain only digits.\n";
}
// iv. email format
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
msg += "Enter a valid email address.\n";
}
// v. interest length 20 - 1000
if (interest.length < 20 || interest.length > 1000) {
msg += "Interest must be between 20 and 1000 characters.\n";
}
if (msg != "") { alert(msg); return false; }
return true; // submit the form
}
</script>
</head>
<body>
<form name="regForm" action="register.php" method="post" onsubmit="return validateForm()">
<table>
<tr><td colspan="2" align="center"><h3>User Registration Form</h3></td></tr>
<tr><td>Name</td><td><input type="text" name="name"></td></tr>
<tr><td>Email</td><td><input type="text" name="email"></td></tr>
<tr><td>Gender</td>
<td><input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female</td></tr>
<tr><td>Phone</td><td><input type="text" name="phone"></td></tr>
<tr><td>Interest</td>
<td><textarea name="interest" rows="5" cols="30"></textarea></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Register"></td></tr>
</table>
</form>
</body>
</html>
Explanation
onsubmit="return validateForm()"runs validation first; returningfalsecancels submission.f.gender.valuereturns the checked radio's value (empty if none).f.name,f.email,f.phoneandf.interestrefer to the form controls with thosenameattributes (a named control takes priority over the form's own property of the same name).- Regular expressions check each rule:
/^[A-Za-z ]+$/letters only,/^[0-9]+$/digits only, and a basic email patterntext@text.text. If the phone must also be exactly 10 digits, use/^[0-9]{10}$/. interest.lengthis compared with 20 and 1000.- The server must validate again, because client-side checks can be bypassed.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- HTML5 IOE examination registration form · Web Technology, chapter 2
- CSS box model margin and padding · Web Technology, chapter 2
- Chi-square: smokers, gender and starting age · Probability and Statistics, chapter 7
- Justify: trending method for large area forecast · Transmission and Distribution System Design, chapter 6
- Design wind pressure, large openings, 100 yr · Design of Steel and Timber Structure, chapter 8
- Multimedia user interface design process · Multimedia System, chapter 6 · asked 2×