# Solved: Javascript: Form validation failed!



## sepala (May 20, 2010)

I created a registration page for a web site and add a javascript to validate it(to make sure no empty fields). The case is, javascript is working fine OK, but the form get submit to the PHP file whether there are empty fields or not. This is my javascript.


```
<script language="javascript">
function validate()
{
    submitOK = "True"
    
    x = document.form1
    userName = x.userName.value
    if (userName==0)
    {
        alert ("The User Name field Cannot remain Blank");
        submitOK = "False"
    }
    
    
    password = x.password.value
    if (password==0)
    {
        alert ("The Password Field cannot remain Blank");
        submitOK = "False"
    }
    
    reEnterPassword = x.reEnterPassword.value
    if (reEnterPassword==0)
    {
        alert ("The Re-Enter Password Field cannot Remain Blank");
        submitOK = "False";
    }
    
    firstName = x.firstName.value
    if (firstName==0)
    {
        alert ("The First Name Field cannot remain Blank")
        submitOK = "False";
    }
    
    lastName = x.lastName.value
    if (lastName==0)
    {
        alert ("The Last Name Field cannot Remain Blank");
        submitOK = "False";
    }
    
    email = x.email.value
    if (email==0)
    {
        alert("The Email Field cannot Remain blank");
        submitOK = "False";
    }
    
    PhoneNumber = x.phoneNumber.value
    if (PhoneNumber==0)
    {
        alert ("The Phone Number Field cannot remain Blank");
        submitOK = "False";
    }
    
    numberOfTickets = x.numberOfTickets.value
    if (numberOfTickets==0)
    {
        alert ("The Number Of Tickets Field cannot remain Blank");
        submitOK = "False";
    }
    else if (isNaN(numberOfTickets))
    {
        alert ("Please enter a valid number for this field");
        submitOK = "False";
    }
    
    numberOfHotelTickets = x.numberOfHotelTickets.value
    if (numberOfHotelTickets==0)
    {
        alert ("The Number of Tikets Field cannot remain Blank");
        submitOK = "False";
    }
    else if (isNaN(numberOfHotelTickets))
    {
        alert ("Please enter a Valid Number for this field");
        submitOK = "False";
    }
    
    verifyEmail = x.email.value.indexOf("@")
    if (verifyEmail==-1)
    {
        alert ("Invalid E-mail");
        submitOK = "False";
    }
    
    if (password!==reEnterPassword)
    {
        alert ("Passwords do not match");
        submitOK = "False";
    }
    
}

</script>
```
This is my form(without css)

```
<table width = "800">[TR]
      [TD][TABLE]
        [TR]
          [TD][CENTER]
[B][SIZE=14][I]Registration Form[/I][/SIZE][/B]

            You can Book Air line and Hotels tickets from here
          [/CENTER][/TD]
        [/TR]
      [/TABLE]
        [TABLE]
          [TR]
            <td width = "200">[B]Login Details[/B][/TD]
            <td width = "200"> [/TD]
            <td width = "200"> [/TD]
            <td width = "200"> [/TD]
          [/TR]
          [TR]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD]User Name[/TD]
            [TD][/TD]
            [TD]Password[/TD]
            [TD][/TD]
          [/TR]
          [TR]
            [TD] [/TD]
            [TD] [/TD]
            [TD]Re-Enter Password[/TD]
            [TD][/TD]
          [/TR]
          [TR]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD][B]Personal Details[/B][/TD]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD]First Name[/TD]
            [TD][/TD]
            [TD]Last Name[/TD]
            [TD][/TD]
          [/TR]
          [TR]
            [TD]E-mail[/TD]
            [TD][/TD]
            [TD]Gender[/TD]
            [TD]MaleFemale[/TD]
          [/TR]
          [TR]
            [TD]Phone Number[/TD]
            [TD][/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD][B]Book an Air line Ticket[/B][/TD]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD]From[/TD]
            [TD]America
            Newzealand
            China
            India
            Sri Lanka
            Pakistan
            Japan
            Korea
            Iran
            Nepal
            Malaysia

            [/TD]
            [TD]To[/TD]
            [TD]America
            China
            Newzealand
            India
            Sri Lanka
            Pakistan
            Japan
            Korea
            Iran
            Nepal
            Malaysia
            [/TD]
          [/TR]
          [TR]
            [TD]Number of Tickets[/TD]
            [TD][/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD][B]Book a Hotel Ticket[/B][/TD]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD]Hotel[/TD]
            [TD]Kingston Hotel
            Hilton
            Amaya
            King's Court
            Rest Hotel
            SkyLine
            Master's Hotel
            [/TD]
            [TD]Number of Tickets[/TD]
            [TD][/TD]
          [/TR]
          [TR]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
            [TD] [/TD]
          [/TR]
          [TR]
            [TD] [/TD]
            [TD][/TD]
            [TD][/TD]
            [TD] [/TD]
          [/TR]
        [/TABLE]
     [/TD][/TR][/TABLE]
```
Please help


----------



## andythepandy (Jul 2, 2006)

As far as I can see, your validate() function doesn't actually return a value. For this to work it needs to return either true or false.
Also, the submitOK variable should hold true or false, not string values.

e.g

```
<script language="javascript">
function validate()
{
    submitOK = true
    
    x = document.form1
    userName = x.userName.value
    if (userName==0)
    {
        alert ("The User Name field Cannot remain Blank");
        submitOK = false
    }
    
    
    password = x.password.value
    if (password==0)
    {
        alert ("The Password Field cannot remain Blank");
        submitOK = false
    }
    
    reEnterPassword = x.reEnterPassword.value
    if (reEnterPassword==0)
    {
        alert ("The Re-Enter Password Field cannot Remain Blank");
        submitOK = false;
    }
    
    firstName = x.firstName.value
    if (firstName==0)
    {
        alert ("The First Name Field cannot remain Blank")
        submitOK = false;
    }
    
    lastName = x.lastName.value
    if (lastName==0)
    {
        alert ("The Last Name Field cannot Remain Blank");
        submitOK = false;
    }
    
    email = x.email.value
    if (email==0)
    {
        alert("The Email Field cannot Remain blank");
        submitOK = false;
    }
    
    PhoneNumber = x.phoneNumber.value
    if (PhoneNumber==0)
    {
        alert ("The Phone Number Field cannot remain Blank");
        submitOK = false;
    }
    
    numberOfTickets = x.numberOfTickets.value
    if (numberOfTickets==0)
    {
        alert ("The Number Of Tickets Field cannot remain Blank");
        submitOK = false;
    }
    else if (isNaN(numberOfTickets))
    {
        alert ("Please enter a valid number for this field");
        submitOK = false;
    }
    
    numberOfHotelTickets = x.numberOfHotelTickets.value
    if (numberOfHotelTickets==0)
    {
        alert ("The Number of Tikets Field cannot remain Blank");
        submitOK = false;
    }
    else if (isNaN(numberOfHotelTickets))
    {
        alert ("Please enter a Valid Number for this field");
        submitOK = false;
    }
    
    verifyEmail = x.email.value.indexOf("@")
    if (verifyEmail==-1)
    {
        alert ("Invalid E-mail");
        submitOK = false;
    }
    
    if (password!==reEnterPassword)
    {
        alert ("Passwords do not match");
        submitOK = false;
    }

    [COLOR="Red"]return submitOK;[/COLOR]
    
}

</script>
```
For more info look at - http://www.w3schools.com/js/js_form_validation.asp

Andy


----------



## sepala (May 20, 2010)

WOW.....Thanks a lot andythepandy..


----------

