SearchSearch   

newbie: form checking function, compare emails

 
   Webmaster Forums (Home) -> Javascript RSS
Next:  shell or system call from JS  
Author Message
ShoeShine

External


Since: Aug 09, 2007
Posts: 1



(Msg. 1) Posted: Thu Aug 09, 2007 7:56 am
Post subject: newbie: form checking function, compare emails
Archived from groups: comp>lang>javascript (more info?)

hi,

I'm trying to write a form-checking function that will ride "inside" a PHP
page that "writes" out an HTML user-input form. Most of the other fields
are checked fine by the PHP processor script, but I"m clueless in PHP so I'm
attempting to insert a little email checker function in the "head" of my doc
that is called by the onBlur function as soon as the focus has left the 2nd
email input field.

There's 2 email input fields, just to compare that the the correct email has
been input, ie, one is exactly equal to the other.

so the script in the HEAD looks like this:

<script language="javascript" type="text/javascript">
function chckEM() {
if (form1.email2.value!=form1.email.value)
{ alert("your email entries did not match ...");
form1.email.focus();
return false;
}
return true;
}
</script>

and it's called by the onBlur in the 2nd email field like this:

<input name="email2" type="text" size="40" maxlength="40"
onBlur="chckEM();">

please let me know if this is a reasonable way of doing things.

thanks,

ssb
Back to top
David Mark

External


Since: Aug 06, 2007
Posts: 358



(Msg. 2) Posted: Thu Aug 09, 2007 7:56 am
Post subject: Re: newbie: form checking function, compare emails
Archived from groups: per prev. post (more info?)

On Aug 9, 3:56 am, "ShoeShine" <Blishter....TakeThisOut@RemarkAble.com> wrote:
> hi,
>
> I'm trying to write a form-checking function that will ride "inside" a PHP
> page that "writes" out an HTML user-input form. Most of the other fields
> are checked fine by the PHP processor script, but I"m clueless in PHP so I'm
> attempting to insert a little email checker function in the "head" of my doc
> that is called by the onBlur function as soon as the focus has left the 2nd
> email input field.

That's your first mistake. Never do validation in an onblur event.
Validate when the form is submitted (onsubmit.)

>
> There's 2 email input fields, just to compare that the the correct email has
> been input, ie, one is exactly equal to the other.
>
> so the script in the HEAD looks like this:
>
> <script language="javascript" type="text/javascript">
> function chckEM() {
> if (form1.email2.value!=form1.email.value)

Use syntax like document.forms['form1'].elements['email2'] to
reference the elements.

> { alert("your email entries did not match ...");
> form1.email.focus();

See above. Also, this illustrates the problem of validating in the
onblur event. What if a user first focuses the email2 element, enters
an address and then attempts to focus the email element? At best they
will get a useless alert.

> return false;

Why are you returning false here? It makes no sense considering that
you focused the email element.

> }
> return true;
> }
> </script>
>
> and it's called by the onBlur in the 2nd email field like this:
>
> <input name="email2" type="text" size="40" maxlength="40"
> onBlur="chckEM();">
>
Back to top
Evertjan.

External


Since: Sep 30, 2005
Posts: 2391



(Msg. 3) Posted: Thu Aug 09, 2007 8:30 am
Post subject: Re: newbie: form checking function, compare emails
Archived from groups: per prev. post (more info?)

ShoeShine wrote on 09 aug 2007 in comp.lang.javascript:

> so the script in the HEAD looks like this:
>
> <script language="javascript" type="text/javascript">

Do not use language="javascript" in the 21st century.

> function chckEM() {
> if (form1.email2.value!=form1.email.value)

For more cross browser compatibility use:

if (document.forms['form1'].elements['email2'].value !=
document.forms['form1'].elements['email'].value)


> { alert("your email entries did not match ...");
> form1.email.focus();
> return false;
> }
> return true;

The return value is NOT used by an input onBlur!!!
Where would you want to use it for?

> }
> </script>
>
> and it's called by the onBlur in the 2nd email field like this:
>
> <input name="email2" type="text" size="40" maxlength="40"
> onBlur="chckEM();">

> ... but I"m clueless in PHP ...

Clientside validating without serverside checking,
however, is not a secure option,
so better learn PHP. [or ASP jscript perhaps?]

===============================

if you persist in calling the function from email2
you could do this:

<input name="email2" type="text" onBlur="chckEM(this);">

<script type='text/javascript'>
function chckEM(elem) {
if (elem.value !=
document.forms['form1'].elements['email'].value) {
alert("your email entries did not match ...");
document.forms['form1'].elements['email'].focus();
};
};
</script>

=================================

Transferring your checking to
<form onsubmit = 'return otherCheckFunction()' ...
will prevent clientside unvalidated submission
using your return values.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Back to top
Display posts from previous:   
       Webmaster Forums (Home) -> Javascript
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum