In: Computer Science
JavaScript Form Validation
Name: a text box where the content must start with 1 upper case characters and no special character (i.e. !, @, #, $, %, &, *). Number is not allowed. The text box must not be empty.
Module code: a text box where the content must start with 3 lower
case alphabets and follows by 4 digits where the first digit cannot
be zero. This textbox can be empty.
All validation error messages must be italic and in red colour and besides the HTML element.
There should be a validation error displayed for every error.
Example:
If the user enters a number and special characters in the Name textbox, a message should say on the side "Numbers are not allowed!" in red and italic.
If the user leaves the name Textbox empty after submission, there should be an error message on the side saying "Name cannot be empty!"
If the user enters uppercase characters followed by the number starting with zero in the Module code TextBox, a message should say on the side "Content must start with 3 lower case alphabets!" followed by "First digit cannot be zero!"
<html>
<body>
Name: <input type="text" id="txtName"/>
<br/>
<br/>
Module code: <input type="text" id="txtName1"/>
<br/>
<br/>
<input type="button" value="Submit"
onclick="Validate()"/>
<script>
function Validate() {
var regex = /^[A-Za-z]+$/ //Regex for Valid Characters i.e.
Alphabets.
var isValid
=regex.test(document.getElementById("txtName").value);//Validate
TextBox value against the Regex.
x=document.getElementById("txtName").value //getting user data from
html
if(document.getElementById("txtName").value == ""){ //if input is
empty
alert("Name cannot be
empty!"); //alert message
}
else{//if input is non empty
if (!isValid) { //if input contain special character (i.e. !, @, #,
$, %, &, *) and Numbers
alert("Numbers are not allowed!"); //alert message
} else {// //if input does not contain special character
var
y=document.getElementById("txtName").value.charAt(0)//starting
character of input
if(y>='A' && y<='Z'){
//if starting with a upper case alphabet
alert("Text is validate.");
}
else{//if starting aplhabet is not a upper case alphabet
alert("Numbers are not allowed!");
}
}
}
var x1=document.getElementById("txtName1").value//getting user data
from html
var x2=x1.substring(0,3);//statrting sub string
var x4=document.getElementById("txtName1").value.charAt(3); //4th
digit to check for zero
var x3=x1.substring(4,6); //sub string
var regex1 = /^[a-z]+$/ //regrex for starting should be lower case
aplhabet
var isValid1 =regex1.test(x2);
var regex2 = /^[0-9]+$/ //regrex for after 3 aplhabets should be
number
var isValid2 =regex2.test(x3);
if(!isValid1) { //if input does not start with 3 alphabets
alert("Content must start
with 3 lower case alphabets!");
}
else{
if(!isValid2){ //if input does start with 3
alphabets
alert("input must be number!");
}
else{//check for 4 digit
if(x4!='0'){ //check for 4th digit is not zero
alert("Accepted");
}
else{ //check for 4th digit is zero
alert("Content must start with 3 lower case alphabets and First
digit cannot be zero!");
}
}
}
return isValid;
}
</script>
</body>
</html>
input:
output :
input 2:
If you found this answer helpful please give a thumbs up.