By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

Validation rule not working

P: n/a
I've ripped off some script from another site of mine that works fine which
checks an input box to see if an email address has been entered when
submitting. If it hasn't a prompt is issued asking the user to enter an
email address.

It seems to be ignoring the validation rule and just emailing the form
anyway, can anyone help?

Below is the the relevant code -

From the <head> tag-

<SCRIPT LANGUAGE="JavaScript"> //This defines the language as javascript
<!-- hide JS code
function validateForm(form)

{
// EMAIL VALIDATION

if (!validateEMail(form.Email.value)) // is the email address valid?
{
form.Email.focus()
return false
}

if (justValidating=true) // the alert box only shows when validating, not
submitting
alert("Thank you. Your data has been validated and is ready to be
submitted.") // message shown if all data is valid
return true
}

// EMAIL VALIDATION RULES

function validateEMail(Email)
{
if (isBlank(Email)) // email blank?
{
alert("Enter your email address, please!") // return this message if it is
blank
return false
}
var atsignPos = Email.indexOf("@", 0) // check for @ sign
if (atsignPos == -1)
{
alert("Enter a valid email address with an @, please!") // return this
message if no @ sign present
return false
}
if (Email.indexOf(".", atsignPos) == -1) // check for full stop after @ sign
{
alert("Enter a valid email domain after the @, please!") //return this
message if no full stop after @ sign
return false
}
return true
}

// end JS hide -->
</SCRIPT>

And then this is the details within the form -

<form name="Validate" action="mailto:dummyemailaddress.co.uk" method="post"
onSubmit="justValidating=false; return validateForm(Validate)">

<input type="text" name="Email">
<input type="submit" name="Submit" value="Submit" ; validateForm(Validate)>

<input type="reset" name="Reset" value="Reset"></form>


Any help appreciated

Thanks

Andrew

Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Andrew wrote:

[snip]
It seems to be ignoring the validation rule and just emailing the form
anyway, can anyone help? [snip] From the <head> tag-
The <head> element - the code certainly doesn't reside within the tag.

<SCRIPT LANGUAGE="JavaScript"> //This defines the language as javascript
You are missing the required type attribute.

<URL:http://www.w3.org/TR/html401/interact/scripts.html#edef-SCRIPT>

<!-- hide JS code
Practically speaking, this does nothing, except increase the possibility for
headaches if you plan to use XHTML in the future. In any case, it's
usually a better idea to put your Javascript in external files.
[snip] if (justValidating=true) // the alert box only shows when validating, not
submitting
I fail to see the difference. Your form as described is only ever validated
when it is submitted.

alert("Thank you. Your data has been validated and is ready to be
submitted.") // message shown if all data is valid
return true
}

// EMAIL VALIDATION RULES

function validateEMail(Email)
{ [snip]

That is fairly convoluted code that gives false positives and false
negatives. Why not just use a couple of regexps instead? There is some
discussion of the issues here:

<URL:http://blog.tom.me.uk/2003/08/03/evil_email_addresses.php>
[snip] <form name="Validate" action="mailto:dummyemailaddress.co.uk"
mailto actions are unreliable.

<URL:http://www.netmechanic.com/news/vol3/form_no4.htm>
[snip] <input type="submit" name="Submit" value="Submit" ;
validateForm(Validate)>
This isn't valid HTML, there is no Javascript seen by the browser here. The
onsubmit attribute of the <form> element takes care of the validation when
the form is submitted anyway.

<input type="reset" name="Reset" value="Reset"></form>


<URL:http://www.useit.com/alertbox/20000416.html>
Good places to look for hints when debugging are the Javascript console in
Mozilla, and the HTML validator service at <URL:http://validator.w3.org/>.

--
Jim Dabell

Jul 20 '05 #2

P: n/a
JRS: In article <OL********************@newsfep4-glfd.server.ntli.net>,
seen in news:comp.lang.javascript, Andrew <andrew-
di**************@ntlworld.com> posted at Fri, 15 Aug 2003 18:31:01 :-
I've ripped off some script from another site of mine that works fine which
checks an input box to see if an email address has been entered when
submitting. If it hasn't a prompt is issued asking the user to enter an
email address.

It seems to be ignoring the validation rule and just emailing the form
anyway, can anyone help? ... if (justValidating=true) // the alert box only shows when validating, not ==
xxxxxsubmitting
alert("Thank you. Your data has been validated and is ready to be
submitted.") // message shown if all data is valid


That's another reason for never testing equality with a Boolean
constant; the better fix is on the second inserted line.

One can do a little better testing E-addresses; use a RegExp to test for
something at something dot something

See in <URL:http://www.merlyn.demon.co.uk/js-other.htm#VEmA>.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.