How to stop a validation function from executing after it encounters an error?

Discussion in 'Javascript' started by effendi@epitome.com.sg, Feb 27, 2005.

  1. Guest

    I wrote a very simple validation fucntion to check if all fields are
    entered properly in a dialog box before returning this the values to
    the parent form. The validation works finr however, I am not sure if I
    am doing this correctly. For example if the telephone number field is
    empty, the alert box appear telling the users to enter the telephone
    number. However, once I click OK. the script appear to run and I get an
    error message in the "dialog box window". What I would like to do is
    let the user click ok and return back to the problem field. Below is my
    code.

    var thisdoc=document.forms[0]
    var Role=getSelectedText(thisdoc.Role)
    var MemberLookup=thisdoc.MemberLookup.value

    if(Role==">>>Select Role") {
    alert("Please select a role for this Team member")
    thisdoc.Role.focus();
    return
    }

    if (Role.substring(0,2)=="01" && thisdoc.TeamLeadAppt.value=="Y"){
    alert("You can appoint only one person as Team Leader")
    }

    var ContactNo=thisdoc.ContactNumber.value

    if(ContactNo.value==" ") {
    alert("Please enter a Contact Number Team member")
    thisdoc.ContactNumber.focus();
    return;
    }
    if(ContactNo.length!=8) {
    alert("Please enter a valid 8 digit telephone number this Team
    member")
    thisdoc.Role.focus();
    return;
    }
    if(ContactNo.length!=8) {
    alert("Please enter a valid 8 digit telephone number this Team
    member")
    thisdoc.Role.focus();
    return;
    }

    window.opener.teamSelectionCallback(MemberLookup,Role,ContactNo)
    window.close()
     
    , Feb 27, 2005
    #1
    1. Advertising

  2. RobG Guest

    Re: How to stop a validation function from executing after it encountersan error?

    wrote:
    > I wrote a very simple validation fucntion to check if all fields are
    > entered properly in a dialog box before returning this the values to
    > the parent form. The validation works finr however, I am not sure if I
    > am doing this correctly. For example if the telephone number field is
    > empty, the alert box appear telling the users to enter the telephone
    > number. However, once I click OK. the script appear to run and I get an
    > error message in the "dialog box window". What I would like to do is
    > let the user click ok and return back to the problem field. Below is my
    > code.
    >
    > var thisdoc=document.forms[0]
    > var Role=getSelectedText(thisdoc.Role)
    > var MemberLookup=thisdoc.MemberLookup.value
    >
    > if(Role==">>>Select Role") {
    > alert("Please select a role for this Team member")
    > thisdoc.Role.focus();
    > return
    > }


    Taking a guess that you are running the validation using
    onsubmit, then use:

    return false;

    and in your onsubmit:

    onsubmit="return validationFun();"

    to ensure the return value is passed to the form. That will stop
    the form submitting when the validation fails. If all goes OK,
    then just let the script end, successful completion will return
    true.

    [...]

    --
    Rob
     
    RobG, Feb 27, 2005
    #2
    1. Advertising

  3. Hello,

    Are you trying to say that the script is entering more than one of your if
    statements?

    Is all of the code below in a single function?

    Rob

    :)

    <> wrote in message
    news:...
    >I wrote a very simple validation fucntion to check if all fields are
    > entered properly in a dialog box before returning this the values to
    > the parent form. The validation works finr however, I am not sure if I
    > am doing this correctly. For example if the telephone number field is
    > empty, the alert box appear telling the users to enter the telephone
    > number. However, once I click OK. the script appear to run and I get an
    > error message in the "dialog box window". What I would like to do is
    > let the user click ok and return back to the problem field. Below is my
    > code.
    >
    > var thisdoc=document.forms[0]
    > var Role=getSelectedText(thisdoc.Role)
    > var MemberLookup=thisdoc.MemberLookup.value
    >
    > if(Role==">>>Select Role") {
    > alert("Please select a role for this Team member")
    > thisdoc.Role.focus();
    > return
    > }
    >
    > if (Role.substring(0,2)=="01" && thisdoc.TeamLeadAppt.value=="Y"){
    > alert("You can appoint only one person as Team Leader")
    > }
    >
    > var ContactNo=thisdoc.ContactNumber.value
    >
    > if(ContactNo.value==" ") {
    > alert("Please enter a Contact Number Team member")
    > thisdoc.ContactNumber.focus();
    > return;
    > }
    > if(ContactNo.length!=8) {
    > alert("Please enter a valid 8 digit telephone number this Team
    > member")
    > thisdoc.Role.focus();
    > return;
    > }
    > if(ContactNo.length!=8) {
    > alert("Please enter a valid 8 digit telephone number this Team
    > member")
    > thisdoc.Role.focus();
    > return;
    > }
    >
    > window.opener.teamSelectionCallback(MemberLookup,Role,ContactNo)
    > window.close()
    >
     
    Robert Mark Bram, Feb 27, 2005
    #3
  4. How to stop a validation function from executing after it encounters an error?

    JRS: In article <>,
    dated Sun, 27 Feb 2005 02:33:58, seen in news:comp.lang.javascript,
    posted :

    >if(ContactNo.length!=8) {
    > alert("Please enter a valid 8 digit telephone number this Team
    >member")


    if (!/^\d{8}$/.test(ContactNo)) {
    alert("Please enter a valid 8 digit telephone number for this Team member")

    should be considerably better.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Feb 27, 2005
    #4
  5. Re: How to stop a validation function from executing after it encounters an error?

    JRS: In article <42235315_1@127.0.0.1>, dated Mon, 28 Feb 2005
    11:21:25, seen in news:comp.lang.javascript, Effendi Baba
    <> posted :
    >
    >Thanks for correcting the error message but what's does your test
    >function do? Does it check the number for valid characters only and that
    >it should only be 8 character long?


    It tests for an 8 decimal digit string, to match the error message. It
    does not check that the number is telephonically appropriate; it accepts
    all of 00000000 to 99999999, and nothing else.

    See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Feb 28, 2005
    #5
  6. Re: How to stop a validation function from executing after it encounters an error?

    JRS: In article <42258fca$1_1@127.0.0.1>, dated Wed, 2 Mar 2005
    04:04:58, seen in news:comp.lang.javascript, Effendi Baba
    <> posted :

    Please learn how to format news replies; start by reading the newsgroup
    FAQ.

    >Thank you very much for this test. I think it will be useful in other
    >situation as well. Could you lead me to a resource that could help me
    >understand how to construct this test?


    If what I have already given you is not enough, I can help no further.

    >*** Sent via Developersdex http://www.developersdex.com ***
    >Don't just participate in USENET...get rewarded for it!


    How do you propose to transfer that reward to those who help you?

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Mar 2, 2005
    #6
    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. Lloyd Sheen
    Replies:
    3
    Views:
    375
    Lloyd Sheen
    Mar 7, 2004
  2. Afshar
    Replies:
    2
    Views:
    371
    Afshar
    Apr 10, 2008
  3. Santosh

    Function is executing before validation

    Santosh, May 17, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    145
    Santosh
    May 17, 2006
  4. Rose
    Replies:
    4
    Views:
    136
    Michele Dondi
    Feb 17, 2008
  5. mak
    Replies:
    4
    Views:
    105
    Evertjan.
    Mar 8, 2008
Loading...

Share This Page