javascript validation for a not required field, field is onlyrequired if another field has a value

Discussion in 'Javascript' started by jr, Jul 8, 2010.

  1. jr

    jr Guest

    The psuedocode is

    1. if "search_bu" is empty
    then return false because it is a required field


    2. if "search_zoneid has a value
    then if search_zonenm is empty return false

    3 OR the reverse of 2
    if "search_zonenm has a value
    then if search_zoneid is emtpy return false


    I think what it is doing know is working on all 3 but if I don't input
    either a zoneid or zonenm it still asks for the zonenumber?????
    It may be number 2 and 3 need to be an OR someway? The problem is
    zoneid or zonenm are not rquired but it is acting like they are
    required.
    They are only required if one of those fields has a value.

    thanks,

    JanisR

    function checkscript() {

    if (document.forms[0].search_bu.value == '' ||
    document.forms[0].search_bu.value== null ) {
    // something else is wrong
    alert('The Business Unit is a required field!');
    return false;
    }
    else if (document.forms[0].search_zoneid.value != '' ||
    document.forms[0].search_zoneid.value!=null) {
    if (document.forms[0].search_zonenm.value == '' ||
    document.forms[0].search_zonenm.value==null ){
    alert('You must input the zone number if you search on
    zone id!');
    return false;
    }

    }
    else if (document.forms[0].search_zonenm.value != '' ||
    document.forms[0].search_zonenm.value!= null) {
    if (document.forms[0].search_zoneid.value == '' ||
    document.forms[0].search_zoneid.value==null ){
    alert('You must input the zone number if you search on
    zone id!');
    return false;
    }

    //}
    // if true you can submit the form

    return true;
    }
    </script>
    jr, Jul 8, 2010
    #1
    1. Advertising

  2. jr

    jr Guest

    On Jul 7, 6:49 pm, Stefan Weiss <> wrote:
    > On 08/07/10 03:36, jr wrote:> The psuedocode is
    >
    > > 1.  if "search_bu"  is empty
    > >      then return false because it is a required field

    >
    > [snip]
    >
    > You already got replies to this question in a different thread, there's
    > no need to start another. It would also make things a lot easier if you
    > could just post a link to your form.
    >
    > --
    > stefan


    the reason I don't have a sample online is I have mysql & php server
    configured for localhost testing and I just changed the configuration
    to allow remote access and the server won't start but I will have it
    ironed out hopefully by tomorrow. The only reason I reposted is these
    spam drug sales messages totally eclipse your first post and you don't
    know if anyone will go back there again. So I think you are saying
    just validate for the required field and I handle the two fields that
    require each other a different way? OkaY, i went back and read it and
    found your answer thansk,
    jr, Jul 8, 2010
    #2
    1. Advertising

  3. jr

    SAM Guest

    Le 7/8/10 3:36 AM, jr a écrit :
    > The psuedocode is


    No interest !

    Read, understand, given answers to you others posts.

    And first, learn to code correctly HTML.
    (before to try to play with JS)

    Learn to use the Errors Consoles of your Navigators.

    Learn to bebbug your scripts,
    how to use JS that can show you from where errors are comming
    try ... catch
    or, at least :

    if (document.forms[0] &&
    document.forms[0].bu &&
    document.forms[0].bu.value &&
    document.forms[0].bu.value.length < 1
    }
    {
    alert('something wrong');
    }

    or, probably better :

    if (!!document.forms[0] )
    {
    var m = 'form is known\n';
    if (!!document.forms[0].bu)
    { alert('element nammed "bu" is known');
    if(!!document.forms[0].bu.value)
    { alert('value of "bu" is known');
    if(!!document.forms[0].bu.value.length < 1)
    {
    alert('"bu" is empty');
    }
    else alert('value o "bu" = '+document.forms[0].bu.value);
    }
    else alert('"bu" has no value');
    }
    else alert('no element "bu" !');
    }
    else alert('No such form in this page');

    --
    sm
    SAM, Jul 8, 2010
    #3
  4. jr

    SAM Guest

    Le 7/8/10 6:53 AM, jr a écrit :
    > On Jul 7, 6:49 pm, Stefan Weiss <> wrote:
    >> On 08/07/10 03:36, jr wrote:> The psuedocode is
    >>
    >>> 1. if "search_bu" is empty
    >>> then return false because it is a required field

    >> [snip]
    >>
    >> You already got replies to this question in a different thread, there's
    >> no need to start another. It would also make things a lot easier if you
    >> could just post a link to your form.

    >
    > the reason I don't have a sample online is I have mysql & php server


    blah !
    can't you try localy on a stand-alone html file ?

    > The only reason I reposted is these
    > spam drug sales messages totally eclipse your first post and you don't


    ???

    can't you clic on subject then ask to your news-reader to sort on that
    subject ?

    can't your remenber wich subjects you yourself launched ?

    Does any one could explain how to plonk that spams ?



    > So I think you are saying
    > just validate for the required field and I handle the two fields that
    > require each other a different way?


    "I" think you have to :
    - get care your fingers don't tape what they have not to tape
    var d = docuement.getElementById('bu');
    will give a JS error
    - take habit to see that 'bu' in the JS script
    can't be 'BU' in html code
    var d = document.getElementById('bu')
    will give a JS error if html code is :
    <div id="BU">blah</div>

    Just to give a JS function that treats elements in html
    without giving that html
    is not enough to debug your work.

    --
    sm
    SAM, Jul 8, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick
    Replies:
    3
    Views:
    414
    Chris Becker
    Jul 7, 2003
  2. =?Utf-8?B?ZGh1cndpdHo=?=
    Replies:
    1
    Views:
    456
    Teemu Keiski
    Apr 10, 2004
  3. Alfonso Alvarez
    Replies:
    0
    Views:
    596
    Alfonso Alvarez
    May 13, 2004
  4. z. f.
    Replies:
    2
    Views:
    474
    Thomas Dodds
    Aug 3, 2004
  5. Just D.
    Replies:
    0
    Views:
    446
    Just D.
    Mar 10, 2006
Loading...

Share This Page