get value from object in JavaScript
How to get value from object in JavaScript?
How to validate email in JavaScript?
visitors name.
Email_Id
gender/sex
the visitors date of birth(dd/mm/yyyy)
text box to retrieve the users hobbies
submit/reset/abort button
Client side validation:
Name,email,sex,Dob con not be empty.
The email address can not contain any special character except '@' and '.'
the length of email and name field can not exceed 30 character
date of birth is keyed in "dd/mm/yyyy" formats and validate day, month and year.
day is between 1 to 31, month is 1 to 12 and year is accept till 2020.
Html/JavaScript
<html>
<head><script language="Javascript">
function verify()
{
let i;
for(i=0;i<7;i++)
{
if(document.forms[0].elements[i].value=="")
{
alert("Please fill in the "+document.forms[0].elements[i].name+" field");
document.forms[0].elements[i].focus();
return(false);
}
if(document.forms[0].elements[i].value!="")
{
pass=document.forms[0].elements[1].value.indexOf('@',0);
pass1=document.forms[0].elements[1].value.indexOf('.',0);
if(pass==-1 ||pass1==-1)
{
alert("Inavlid Email-id");
document.forms[0].elements[1].focus();
return(false);
}
}
}
return(true);
}
function checkDate()
{
year=document.forms[0].elements[6].value;
if(year>2020)
{
alert("Enter correct year(till 2020)");
document.forms[0].elements[6].focus();
return(false);
}
month=document.forms[0].elements[5].value;
date=document.forms[0].elements[4].value;
if(date>31|| date<1)
{
alert("enter proper date");
document.forms[0].elements[4].value="";
document.forms[0].elements[4].focus();
return(false);
}
if(isNaN(month)!=true)
{
if(month>12||month<1)
{
alert("enter proper month");
document.forms[0].elements[5].value="";
document.forms[0].elements[5].focus();
return(false);
}
}
else {
alert("enter month number");
document.forms[0].elements[5].value="";
document.forms[0].elements[5].focus();
return(false);
}
return(true);
}
function checkLength()
{
let i;
for(i=0;i<1;i++)
{
val=document.forms[0].elements[i].value;
len=val.length;
if(len>30)
{
alert("value Exceeds 30 character");
document.forms[0].elements[i].value="";
document.forms[0].elements[i].focus();
return(false);
}
}
}
<!--this function takes user to the previous page-->
function abort(form){
history.back();
}
</script>
</head>
<BODY bgcolor="pink">
<center><h1>My_Shop</h1>
<hr>
<form onSubmit="return verify()">
<b>Please input your name</b><br>
<input type="text" Name="name" size="40" onBlur="checkLength();"/><br>
<b>Please input your Email-id</b><br>
<input type="text" Name="emailid" size="40" onBlur="checkLength();" /><br>
<b>Pleas input your Sex/Gender</b><br>
<input type="radio" Name="gender" value="male" checked>Male
<input type="radio" Name="gender" value="femail">Femail<br>
<b>Please input your date of birth</b><br>
<input type="text" Name="day" size="3" > <input type="text" Name="month" size="3"> <input type="text" Name="year" size="5"><br>
<p>Tell us about your hobbies and interests in the'text-box' bellow</p>
<textarea Name="request" rows="8" cols="65" onfocus="checkDate()"></textarea>
<p>Can we contact you with information about our product or services<br>
<input type="radio" Name="moreInfo" vlaue="y"><span style="color:blue">Yes</span>
<input type="radio" Name="moreInfo" vlaue="n" checked="true"><span style="color:red">No, thanks!</span>
</p>
<p><center> <input type="Submit" value="Submit" Name="Submit"/>
<input type="Reset" value="Reset" Name="Reset"/>
<input type="Button" value="Abort" Name="At" onClick="abort(this.form)" />
</center>
</p>
</form>
<hr>
<p><b>Thank for Shopping by our webSite</b></p>
</center>
</BODY>
</html>
JavaScript code contain four function
1.verify():- The function verify() checks whether appropriate intervention is all the form elements. if element is empty an alert box is displayed to informing the user to fill the blank field and also search the email for '@' and a '.' symbol.
2.checkDate():-The function checkDate() verify the day between(1-31) and month(1-12) and year(till 2020).
3.checkLenght():-The function checkLen() check the lenght of name and email.
thank you
Comments
Post a Comment