Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  174] [ 5]  / answers: 1 / hits: 38603  / 12 Years ago, thu, november 29, 2012, 12:00:00

I have a problem with a validate function, I made it in one function only and I want to add validation of email such as @ and numbers 1-9. How do I add it?



html:



<form onsubmit=return validate(); name=formValidation>
<label>First Name:</label>
<input type=text name=firstName /><br /><br />
<label>Last Name:</label>
<input type=text name=lastName /><br /><br />
<label>E_mail:</label>
<input type=text name=Email /><br /><br />
<label>Confirm E_mail:</label>
<input type=text name=confirmEmail /><br /><br />
<label>Address:</label>
<input type=text name=Address /><br /><br />
<label>Telephone nr:</label>
<input type=text name=telephoneNr /><br /><br />
<br />
<p>submit your form: </p><input type=submit value=Submit />
</form>


js:



function validate(){
if(document.formValidation.firstName.value == ||
document.formValidation.lastName.value == ||
document.formValidation.Email.value == ||
document.formValidation.confirmEmail.value == ||
document.formValidation.Address.value == ||
document.formValidation.telephoneNr.value == )
{
alert(Please fill all the boxes before submitting!);
return false;
} else {
alert('Your form has been submitted!');
}

}

More From » html

 Answers
24

from http://www.w3resource.com/javascript/form/email-validation.php:



This function will validate an email using javascript:



function ValidateEmail(mail)   
{
if (/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(mail))
{
return (true)
}
alert(You have entered an invalid email address!)
return (false)
}


To validate a phone number:



function validatePhone(phone) {
var error = ;
var stripped = phone.replace(/[().- ]/g, '');

if (stripped == ) {
error = You didn't enter a phone number.;
} else if (isNaN(parseInt(stripped))) {
phone = ;
error = The phone number contains illegal characters.;

} else if (!(stripped.length == 10)) {
phone = ;
error = The phone number is the wrong length. Make sure you included an area code.n;
}
}

[#81716] Wednesday, November 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaredsages

Total Points: 273
Total Questions: 97
Total Answers: 105

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
jaredsages questions
;