SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validate script doesn't stay on the same page when return false

    Hi, I've got a fairly simple problem, but I'm fairly new to javascript and not sure why its doing it. I have a validation script which checks properly but when it finds a false field it just goes to the next page rather than staying on the same page to correct the details. Can anyone see where I'm going wrong here?

    Code:
    <script type="text/javascript">
    function validate_email(field,alerttxt)
    {
    with (field)
    {
    apos=value.indexOf("@")
    dotpos=value.lastIndexOf(".")
    if (apos<1||dotpos-apos<2) 
      {alert(alerttxt);return false}
    else {return true}
    }
    }
    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_email(CustomerEmail,"Not a valid e-mail address!")==false)
      {email.focus();return false}
    else if (validate_required(CustomerName,"Customer Name must be filled out!")==false)
      {email.focus();return false}
    else if (validate_required(DeliveryAddress,"Delivery Address must be filled out!")==false)
      {email.focus();return false}
    else if (validate_required(DeliveryPostCode,"Delivery PostCode must be filled out!")==false)
      {email.focus();return false}
    else if (validate_required(BillingAddress,"Billing Address must be filled out!")==false)
      {email.focus();return false}
    else if (validate_required(BillingPostCode,"Billing PostCode must be filled out!")==false)
      {email.focus();return false}
    }
    }
    </script>
    Code:
    <form action="customerdetails2.php" method="post" onsubmit="return validate_form(this);">
       Your Email:<br />
      <input maxlength="200" name="CustomerEmail" size="50" value="" /><br /><br />
      Your Name:<br />
      <input maxlength="30" name="CustomerName" size="30" value="" /><br /><br />
      Delivery address:<br />
      <textarea rows="4" name="DeliveryAddress" cols="40"></textarea><br /><br />
      Delivery Postcode:<br />
      <input type="text" name="DeliveryPostCode" size="20" maxlength="20" /><br /><br />
      Billing address:<br />
      <textarea rows="4" name="BillingAddress" cols="40"></textarea><br /><br />
      Billing Postcode:<br />
      <input type="text" name="BillingPostCode" size="20" maxlength="20" /><br /><br />
      <input type="submit" name="submit" value="Next"  />
    </form>

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead
    Code JavaScript:
    if (validate_email(CustomerEmail,"Not a valid e-mail address!")==false)
    try to use
    Code JavaScript:
    if (!validate_email(CustomerEmail,"Not a valid e-mail address!"))
    and if you want to check if it's true use
    Code JavaScript:
    if (validate_email(CustomerEmail,"Not a valid e-mail address!"))

    then
    Code JavaScript:
    email.focus()
    I don't see any field with name 'email'.

    Also instead with() statement I would use document.getElementById()

    Hope it helps.

  3. #3
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers bosko, got it working!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •