Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I am trying to build my self a simple JavaScript form and have the “email address” and “name” confirmed by the user when they click submit, then have the data outputted along with the month,day,year.
I have got most of the form done but don’t know how to progress, if any one can lend a hand then that would be grate.
--Code--
<html>
<head><title>Form Javascript</title>
<link rel="StyleSheet" type="text/css" href="headline.css"><script type="text/javascript">
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
function checkmail(e){
var returnval=emailfilter.test(e.value)
if (returnval==false){
alert("Please enter a valid email address.")
e.select()
}
return returnval
}</script>
</head>
<body>
<H1>Simple Form</H1><form>
First Name: <input type="text" name="myName" size="40" maxlength="256"></P>
Second Name: <input type="text" name="surname" size="40" maxlength="256"></P>
Age range :
<select name="Age"><option value="p">16-21</option>
<option value="p">21-25</option>
<option value="p">25+</option>
</select></P>
Email Address: <input type="text" name="myemail" " style="width: 270px">
<input type="submit" onClick="return checkmail(this.form.myemail)" value="Submit" />
<input type="reset" value="Reset"></P>
</form>
</body>
</html>

<html>
<head><title>Form Javascript</title>
<link rel="StyleSheet" type="text/css" href="headline.css"><script type="text/javascript">
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
function checkmail(fieldID){
email = document.getElementById(fieldID);
var returnval=emailfilter.test(email.value)
if (returnval==false){
alert("Please enter a valid email address.")
email.select()
}
return returnval
}function checkName(fieldID) {
nameField = document.getElementById(fieldID);
//Trim the value
nameField.value = nameField.value.replace(/^\s+|\s+$/g, '');if (!nameField.value) {
alert("Please enter a valid name.")
nameField.select()
return false;
}
return true;
}function checkForm() {
if (!checkName('myName')) { return false; }
if (!checkName('surname')) { return false; }
if (!checkmail('myemail')) { return false; }var htmlOutput = "<H1>Your results</H1>";
htmlOutput += "
First Name: " + document.getElementById('myName').value;
htmlOutput += "
Last Name: " + document.getElementById('surname').value;
htmlOutput += "
Age Range: " + document.getElementById('Age').value;
htmlOutput += "
Email Address: " + document.getElementById('myemail').value;document.getElementById('theBody').innerHTML = htmlOutput;
}
</script></head>
<body id="theBody">
<H1>Simple Form</H1>
<form onsubmit="checkForm(this);return false;">
First Name: <input type="text" name="myName" id="myName" size="40" maxlength="256"></P>
Second Name: <input type="text" name="surname" id="surname" size="40" maxlength="256"></P>Age range :
<select name="Age" id="Age">
<option value="16-21">16-21</option>
<option value="21-25">21-25</option>
<option value="25+">25+</option>
</select></P>Email Address: <input type="text" name="myemail" id="myemail" style="width: 270px">
<input type="submit" value="Submit" />
<input type="reset" value="Reset"></P></form>
</body>
</html>
Michael J

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |