problem with validation script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KathyB

    problem with validation script

    Hi, I need to validate a hi/low range, allowing decimal numbers. For
    this example, the low is 1, high is 5, but something is not working. I
    got this script from someone helping me in this group, but I must have
    done something wrong. When I enter 7, it prompts me as an incorrect
    entry, as expected. However, when I enter 22 or 23, etc. the script
    passes those entries (perhaps accepting individual numbers between 1
    and 5. Sorry, but the regular expression stuff is hardly my strong
    suit...

    Please tell me where I've gone astray! Thanks, Kathy

    function ValidateSave(fo rmRef,fieldName ,min,max)var formField =
    formRef.element s[fieldName];

    if (!/^\d+(\.\d+)?$/.test(formField .value)) {
    alert('Invalid entry...please try again!');
    formField.focus ();
    formField.selec t();
    return false;
    }
    if ((formField.val ue < min) || (formField.valu e > max)) {
    alert("The entry must be between " +min+ " and " +max+ ".");
    formField.focus ();
    formField.selec t();
    return false;
    }
  • Lee

    #2
    Re: problem with validation script

    KathyB said:[color=blue]
    >
    >Hi, I need to validate a hi/low range, allowing decimal numbers. For
    >this example, the low is 1, high is 5, but something is not working. I
    >got this script from someone helping me in this group, but I must have
    >done something wrong. When I enter 7, it prompts me as an incorrect
    >entry, as expected. However, when I enter 22 or 23, etc. the script
    >passes those entries (perhaps accepting individual numbers between 1
    >and 5. Sorry, but the regular expression stuff is hardly my strong
    >suit...
    >
    >Please tell me where I've gone astray! Thanks, Kathy
    >[/color]
    [color=blue]
    >if ((formField.val ue < min) || (formField.valu e > max)) {[/color]

    The problem is not with the regular expressions, but with the
    comparisons. Field values are strings, and so the comparisons
    are being done as string comparisons, and the string "22" is
    less than the string "5".

    You need to convert the values to integers before comparing.
    One simple way to do that is to apply the unary plus sign to
    them. Since that operator only applies to numbers, the engine
    will automatically convert the values to numbers for you:

    if ((+formField.va lue < min) || (+formField.val ue > max))

    Comment

    • Kathy Burke

      #3
      Re: problem with validation script

      THANK YOU, Lee!!!

      Kathy

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

      Comment

      • Michael Winter

        #4
        Re: problem with validation script

        Lee wrote on 24 Nov 2003:

        <snip>
        [color=blue]
        > You need to convert the values to integers before comparing.
        > One simple way to do that is to apply the unary plus sign to
        > them. Since that operator only applies to numbers, the engine
        > will automatically convert the values to numbers for you:
        >
        > if ((+formField.va lue < min) || (+formField.val ue > max))[/color]

        Alternatively, you could use the constructor of the Number object:

        if (( Number( formField.value ) < min )
        || ( Number( formField.value ) > max )) {
        }

        I personally prefer the second, though they both result in the same
        behaviour. Using Number is a little more readable, and doesn't run
        the risk of doing something unintended, like concatenating (if you
        placed another argument in front of the plus in Lee's example).

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.uk.invalid (remove ".invalid" to reply)

        Comment

        • Albert Wagner

          #5
          Re: problem with validation script

          On Mon, 24 Nov 2003 22:42:34 GMT
          Michael Winter <M.Winter@bluey onder.co.uk.inv alid> wrote:
          <snip>[color=blue]
          > Alternatively, you could use the constructor of the Number object:
          >
          > if (( Number( formField.value ) < min )
          > || ( Number( formField.value ) > max )) {
          > }
          >
          > I personally prefer the second, though they both result in the same
          > behaviour. Using Number is a little more readable, and doesn't run
          > the risk of doing something unintended, like concatenating (if you
          > placed another argument in front of the plus in Lee's example).[/color]

          I agree. I always prefer the obvious to non-intuitive idioms.

          --
          Then there was the man who drowned crossing a stream with an average
          depth of six inches.
          -- W. I. E. Gates

          Comment

          • Dr John Stockton

            #6
            Re: problem with validation script

            JRS: In article <Xns943DE7030CA F6MWinterBlueyo nder@193.38.113 .46>, seen
            in news:comp.lang. javascript, Michael Winter <M.Winter@bluey onder.co.uk.
            invalid> posted at Mon, 24 Nov 2003 22:42:34 :-[color=blue]
            >
            >Alternativel y, you could use the constructor of the Number object:
            >
            >if (( Number( formField.value ) < min )
            > || ( Number( formField.value ) > max )) {
            >}[/color]

            Rather than that, I suggest

            var T = Number( formField.value ) // or +formField.valu e
            if (T < min || T > max ) {
            }

            IMHO, once one is accustomed to seeing such as

            var T = +formField.valu e

            one will on reading it readily see that it is a means of getting what is
            wanted (a number) from what is available (a field, holding a string),
            and will not be over-tempted to fiddle with it inappropriately .

            AFAICS, a + directly after an assignment operator must be a unary
            arithmetic operator, whose only purpose can be conversion to number.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            Working...