Experiment No 12
Experiment No 12
Experiment No 12
12
Design a web page with 2 textboxes to accept name and mobile number from user.
Validate both the form field values using regular expressions.
CODE:
<!--
Aniket Gholap
30
EXP 12
-->
<!DOCTYPE html>
<html>
<head>
<title> Regular Expressions </title>
</head>
<body>
<div class = “container-form”>
<form action = “ ” name = “frm1”>
<label for = “name” id = “name”> Name </label>
<input type = “text” placeholder = “John” id = “name-of-
person” maxlength = “30”> <br> <br>
<label for = “mo-no” id = “mobile”> Mobile Number
</label>
<input type = “text” placeholder = “XXXXXX2451” id =
“mobile-number” maxlength = “10”> <br> <br>
<input type = “button” value = “Submit” id = “form-submit”
onclick = “validate()”>
</form>
</div>
<script>
function validate() {
var name = document.getElementById(“name-of-
person”).value;
var regex1 = /^[a-zA-Z ]{1,30}$/;
if (regex1.test(name)) {
alert(“Valid Name”);
} else {
alert(“Invalid Name”);
}
if (regex2.test(number)) {
alert(“Valid Number”);
} else {
alert(“Invalid Number”);
}
}
</script>
</body>
</html>
--------------------------------------------------------------------------------------------------------
OUTPUT:
As Invalid Inputs As Valid Inputs
--------------------------------------------------------------------------------------------------------