Basic Form Validation

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
techkid
Forum Commoner
Posts: 54
Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives

Basic Form Validation

Post by techkid »

I've a issue with Basic Form Validation.

In Dreamwaver It says, There is a Syntax Error on 26th Line. How am I supposed to fix it. My Validation is not working at all.

Code: Select all

<html>
<head>
<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}

function validate_form(thisform)
{
with (thisform)
  {
  if (validate_required(email,"Email must be filled out!")==false)
  {email.focus();return false;}
  }
  else if (validate_required(name,"Name required")==false){
	  name.focus(); return false;
  }
}
</script>
</head>

<body>
<form action="submit.htm" onsubmit="return validate_form(this)" method="post">
Email: <input type="text" name="email" size="30"><br />
Name: <input type="text" name="name" size="30">
<input type="submit" value="Submit">
</form>
</body>

</html>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Basic Form Validation

Post by McInfo »

Does this help you see the problem?

Code: Select all

function validate_form (thisform)
{
    with (thisform)
    {
        if (validate_required(email, "Email must be filled out!") == false)
        {
            email.focus();
            return false;
        }
    }
    else if (validate_required(name, "Name required") == false)
    {
        name.focus();
        return false;
    }
}
User avatar
techkid
Forum Commoner
Posts: 54
Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives

Re: Basic Form Validation

Post by techkid »

Doesn't work.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Basic Form Validation

Post by McInfo »

I didn't give you a solution. I reformatted your code so you can see that the else is at the same nesting level as the with instead of the if.
Post Reply