# Solved: Form Validation Using JavaScript



## pcpro17 (Oct 9, 2006)

Hello. I'm trying to write some JavaScript to validate my form.


```
<script type="text/javascript">
               <!--
               function checkValidEmail()
               {
                  if ( (document.getElementsByName('email').indexOf('@') == 0) || (document.getElementsByName('email') == "") )
                  {
                     alert( "ERROR: Email missing!" );
                  }
               }
               //-->
               </script>
```
Unfortunately, it is not falling into the alert condition when I test it. Any suggestions?
Thanks.


----------



## pcpro17 (Oct 9, 2006)

Here's the bit of form I'm trying to evaluate:


```
<form action="/mail.php" method="post">
...
   <input id="email" class="text" type="text" name="Email" onBlur="javascript:checkValidEmail();" maxlength="50" />
...
</form>
```
Would I be better off just giving form elements and ID and doing something like:


```
<script type="text/javascript">
<!--
   function checkValidEmail()
   {
      if ( (document.email == 0) || document.email == "") )
      {
         alert( "ERROR: Email missing!" );
      }
   }
   //-->
</script>
```
Thanks!


----------



## MMJ (Oct 15, 2006)

onBlur="javascript:checkValidEmail();"

won't work.

onBlur="checkValidEmail();"


----------



## pcpro17 (Oct 9, 2006)

I think either will work. I've been using the former method for years.


----------



## pcpro17 (Oct 9, 2006)

Hey, we joined the same month!


----------



## MMJ (Oct 15, 2006)

pcpro17 said:


> I think either will work. I've been using the former method for years.


coulda sworn that doesn't work, my bad.



pcpro17 said:


> Hey, we joined the same month!


lol


----------



## Eriksrocks (Aug 7, 2005)

You might be better off using a prebuilt Javascript validation library like these:
http://zendold.lojcomm.com.br/fvalidator/
http://www.livevalidation.com/


----------



## DaBeers (Oct 31, 2008)

I see a check for an @ and a null.. There is much better validation code on the internet if you look. Is this for an assignment or practical use?


----------



## Eriksrocks (Aug 7, 2005)

Anyways I found the problem in your original code. It should be:

```
<script type="text/javascript">
<!--
function checkValidEmail()
{
   if ( (document.getElementsByName('email').[B][COLOR="Red"]value[/COLOR][/B].indexOf('@') == 0) || (document.getElementsByName('email').[B][COLOR="Red"]value[/COLOR][/B] == "") )
   {
      alert( "ERROR: Email missing!" );
   }
}
//-->
</script>
```


----------



## pcpro17 (Oct 9, 2006)

I've tried various combos, but it's still not working. Can someone please tell me what I'm doing wrong?

http://mercy.jimfell.net/contact.php

Thanks.


----------



## JimmySeal (Sep 25, 2007)

document.getElementsByName('email') returns an array of elements, so

document.getElementsByName('email').value
and
document.getElementsByName('email').value.indexOf('@')

both have no meaning. If you want to refer to that element you must either use an array index:

document.getElementsByName('email')[0]

or give it an *id* attribute and call it by id:

document.getElementById('email')

Next, value.indexOf('@') will only be 0 if @ is the first character in the text box. If you want to check whether the text box has no @ character in it, you need to check whether indexOf('@') is -1.


----------



## pcpro17 (Oct 9, 2006)

Thanks Jimmy! That was a huge help!


----------

