I really, really need some help with form validation. I have tried over 20 codes to get an email address to validate but none of them work. I'm not very good with Javascript! As far as I can see I am placing the function in the correct place and also calling it at the right time. I would be greatful for any help any one could give me.
You may try this very very simple email validator:
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
function validate(f){
var p=0;
var v = new Array()
v=f.email.value.split('');
for (var i=0;i<v.length;i++){
if((v[i]=='@')||(v[i]=='.')){
p++
}
}
if(p<2){
alert('Please type a valide e-mail');
f.email.focus();
return false
}
}
</script>
</head>
<body>
<form onsubmit="return validate(this)">
e-mail <input name="email" type="text">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Well, it is not a complete validator (the email validator is a really complex problem), but it covers all the possibilities but only according to the following presumtions:
1. the minimum mail string type is something like 'name@domain.extension' (it have at least one "@" and one "." characters)
2. the most common user's mistakes are
- omitting the input
- omitting the extension (that means the dot)
- omitting the @
3. users are well intentioned people and their omitions are accidentally ones, by pure mistake
Sorry, was a bit tired been at it for over 10 hours. Told you I was not very good at it. Its a shopping cart so the form is for customers to order their good. So they submit all their details like name, address, credit card details, email and at the end their is a submit button to submit the order. The order is submitting fine but I can't get anything to validate like poscode, credit card details and email address. I would like a pop up alert telling them if anything is incorrect. I have just tried the code but it does not work.
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
function validate(f){
var p=0;
var v = new Array()
v=f.email.value.split('');
for (var i=0;i<v.length;i++){
if((v[i]=='@')||(v[i]=='.')){
p++
}
}
if(p<2){
alert('Please type a valide e-mail');
f.email.focus();
return false
}
}
</script>
</head>
<body>
<form onsubmit="return validate(this)">
e-mail <input name="email" type="text">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Thanks for this but I'm afraid it did not work for me.
I beg your pardon??? It should work on every browsers I know. At least it works for what I have now installed (NS6, NS7, Mozilla 1.7, Firefox, Opera 5) on XP on PC ...
It validates the classic way. If there is no email or if the string has not a '@' and a '.', an alert message apperas and the submit is stopped. If not, the data is submitted.
Yes I want the alert when you press the submit button. I can see the see this one you gave me but still nothing with the first piece of code. I have this in at the top
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<link rel=stylesheet type="text/css" href="JLstyle.css">
<BODY>
<IMG SRC="crakinlogo.gif" width=100% height=150><BR>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
function validate(f){
var p=0;
var v = new Array()
v=f.email.value.split('');
for (var i=0;i<v.length;i++){
if((v[i]=='@')||(v[i]=='.')){
p++
}
}
if(p<2){
alert('Please type a valide e-mail');
f.email.focus();
return false
}
}
Then in my form I have this
<TD><B class="first">Email Address:</B></TD>
<TD><INPUT TYPE="EMAIL" NAME="email" SIZE=30 onsubmit="return validate(this)"></TD>
onsumit event handler is to be used only on [b]FORM[/] tag. not on on input. I have gave you an example which you have modified, thatis why you got no proper answer. Review my example, please.
I'm sorry but I don't know what you mean. This is my first time doing Javascript and I don't really understand it. Do you mean that my form can't be in a table if I want to validate it?
The table is not the problem. You are referencing the validation function from the wrong point in your html - on the input tag within the form rather than form tag itself.
Your opening form tag should look like something like this...
Code:
<form onsubmit="return validate(this)"...
Applying onsubmit to a single input has no effect because you are actually submitting the form which then collects all the data from your inputs.