SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast archive's Avatar
    Join Date
    Mar 2007
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem with form validation

    Hi people!

    I have a form that I want to validate, and thus I use the following script:

    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(firstname,"msg!")==false)
    {firstname.focus();return false}
    else if (validate_required(lastname,"msg!")==false)
    {lastname.focus();return false}
    else if (validate_required(address,"msg!")==false)
    {address.focus();return false}
    else if (validate_required(area,"msg!")==false)
    {area.focus();return false}
    else if (validate_required(city,"msg!")==false)
    {city.focus();return false}
    else if (validate_required(postalcode,"msg!")==false)
    {postalcode.focus();return false}
    }
    }


    But i also want to check if the telephone number is in 10-digit form, and i add the following:

    function validate_required_tel(field,alerttxt)
    {with (field)
    {
    if (field.value.length!="10")
    {alert(alerttxt);return false}
    else {return true}
    }
    }

    function validate_form(thisform)
    {with (thisform)
    {
    if (validate_required_tel(cellphone,"msg!")==false)
    {cellphone.focus();return false}
    }
    }


    My problem is that any of the two scripts works fine on it's own, but when used together (in the same validation.js page) the first controls are skipped, and only the telephone control works.

    Any ideas??

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats because you can't have two functions of the same name and expect them both to work. Best thing to do is to add the extra code to the first function.


  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    two functions???
    i doubt.

    Try to change them and may work


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
  •