Click to See Complete Forum and Search --> : validation


denice21
10-08-2003, 01:55 PM
I'm new at this! I've put some script on my form to make sure all the required fields are entered, (it works :D ) How do I then validate the email address entered by the user???? :confused:

Charles
10-08-2003, 02:15 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
String.prototype.isEmailAddress = function () {return this.match(/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/)}
// -->
</script>

<form action="">
<div>
<label>Email Address<input type="text" onchange="if (!this.value.isEmailAddress()) {alert('That would not appear to be a valid email address.'); this.value=''; this.focus()}"></label>
<button type="submit">Submit</button>
</div>
</form>

denice21
10-08-2003, 02:28 PM
Charles,

Thanks for your quick response! I have a nother question, since I am calling the 1st validation function on submit, can i just call the email function right after it? If I can, what's the correct syntax?

Charles
10-08-2003, 02:33 PM
The syntax will depend on exactly you are calling your validation function. But reconsider your plans. It is good to validate the content of your form elements as they are filled out, giving the user immediate feedback. And the code is cleaner as a result.

cmvia
12-02-2003, 09:59 AM
Hello!
I have been doing some searching for validation techniques for my form. I came across this thread and I really like and was able to employ the validation of the email address quite easily.

However, I was wondering if it is possible to validate the existance of a value in a field the same way? (I am pretty Javascript illiterate!)

I tried the following:
<input type="text" name="fname" onChange="if (this.value == "" || this.value==" "){alert('Please enter a First Name'); this.focus()}">

Like I said, I am pretty fuzzy on Javascript, so please don't laugh at any obvious problems! (I am thinking the "" maybe screws something up, but, like I said, illiterate! ):D

Thanks Much!
Christine

Pittimann
12-02-2003, 12:20 PM
Hi, cmvia!

You're right: the "" screws something up.

Pleas try it like that with single quotes:
<input type="text" name="fname" onBlur="if (this.value == '' || this.value==' '){alert('Please enter a First Name'); this.focus()}">

Cheers - Pit

cmvia
12-02-2003, 12:35 PM
Thanks...I tried that, but it didn't work. However, in a doh! moment, I realized, that using cold fusion, which is the language i am basing my pages on, this can be done quite easily.
Sometimes I make things so hard for myself!
Thanks so much anyway!!
Christine