field validation needs to accept decimal values -- how please?

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

    field validation needs to accept decimal values -- how please?

    I need this function to accept decimal values (e.g., 2.5 ok, not just
    whole numbers between 1 and 5).

    I can't find this answer...

    thanks, kat

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

    if((parseInt(fo rmField.value) != formField.value ) || formField.value <
    min || formField.value > max)
    {
    alert("The entry must be between " +min+ " and " +max+ ".");
    formField.focus ();
    formField.selec t();
    return false;
    }
    ....else code left out
  • Janwillem Borleffs

    #2
    Re: field validation needs to accept decimal values -- how please?


    "KathyB" <KathyBurke40@a ttbi.com> schreef in bericht
    news:75e8d381.0 310051231.1b53a f28@posting.goo gle.com...[color=blue]
    > I need this function to accept decimal values (e.g., 2.5 ok, not just
    > whole numbers between 1 and 5).
    >[/color]

    I would use a regular expression for this, e.g.:

    var figure = '1.1';
    var reg = /^\d+(\.\d+)?$/;

    alert(reg.test( figure)); // alerts true

    With this, your function might look like this:

    function ValidateSave(fo rmRef,fieldName ,min,max) {
    var formField = formRef.element s[fieldName];
    if (!/^\d+(\.\d+)?$/.test(formField )) {
    alert('Invalid entry!');
    formField.focus ();
    formField.selec t();
    return false;
    }

    if (formField.valu e < min || formField.value > max) {
    alert("The entry must be between " +min+ " and " +max+ ".");
    formField.focus ();
    formField.selec t();
    return false;
    }
    }


    JW



    Comment

    • Janwillem Borleffs

      #3
      Re: field validation needs to accept decimal values -- how please?


      "Janwillem Borleffs" <jw@jwscripts.c om> schreef in bericht
      news:3f80832c$0 $28889$1b62eedf @news.euronet.n l...[color=blue]
      >
      > I would use a regular expression for this, e.g.:
      >[/color]

      Made an error in my code, please use the following:

      function ValidateSave(fo rmRef,fieldName ,min,max) {
      var formField = formRef.element s[fieldName];
      if (!/^\d+(\.\d+)?$/.test(formField .value)) {
      alert('Invalid entry!');
      formField.focus ();
      formField.selec t();
      return false;
      }

      if (formField.valu e < min || formField.value > max) {
      alert("The entry must be between " +min+ " and " +max+ ".");
      formField.focus ();
      formField.selec t();
      return false;
      }
      }


      JW



      Comment

      • Kathy Burke

        #4
        Re: field validation needs to accept decimal values -- how please?

        JW, thanks so much! That works great. I've seen reg expressions
        before...but clearly need to go study them a bit.

        Thanks again.

        Kathy

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

        Comment

        • Kathy Burke

          #5
          Re: field validation needs to accept decimal values -- how please?

          Hi again, JW. For some reason, I'm getting an error on this
          function...look s ok to me, but the parser (Xselerator) says I'm missing
          a "[" or a "(" -- can't tell which. Doesn't give me the exact line, but
          when I remove this function, I don't get the error.

          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.value < min || formField.value > max {
          alert("The entry must be between " +min+ " and " +max+ ".");
          formField.focus ();
          formField.selec t();
          return false;
          }
          else
          {
          varScroll = document.body.s crollTop;
          document.cookie ="position="+va rScroll;
          return confirm("You are about to enter: " +formField.valu e+ " --
          click OK to proceed.");
          }
          }

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

          Comment

          • Janwillem Borleffs

            #6
            Re: field validation needs to accept decimal values -- how please?


            "Kathy Burke" <kathyburke40@a ttbi.com> schreef in bericht
            news:3f8159d7$0 $196$75868355@n ews.frii.net...[color=blue]
            >
            > function ValidateSave(fo rmRef,fieldName ,min,max)
            > {[/color]
            .....[color=blue]
            > }
            > if formField.value < min || formField.value > max {
            >[/color]

            Hi Kathy,

            The if statement above is where the problem is, it should read:

            if (formField.valu e < min || formField.value > max) {


            JW



            Comment

            • Kathy Burke

              #7
              Re: field validation needs to accept decimal values -- how please?

              Thanks again! Guess I was too tired.

              Kathy

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

              Comment

              Working...