Web Analytics Made Easy -
StatCounter input validation.. - CodingForum

Announcement

Collapse
No announcement yet.

input validation..

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • input validation..

    hi,

    I need to be able to accept only numbers and a decimal point in my form... but I dont know how to use that expression thing to filter out other things..

    I already have an onchange event which calls the clean function.... what would I need to put to alert the user that they have put in incorrect data?

    eg:

    alert("You appear to have entered incorrect data\n\nPlease only use numbers 0-9 and decimal points")

    thanks for any help...
    www.mattfacer.com

  • #2
    The answer to frequently asked questions can be quickly found using the forums Search feature.

    .....Willy

    Comment


    • #3
      sorry dude should have done that first....
      I searched, and managed to put together the following code...

      it works perfectly (ie: only allows numbers and decimal) but for some reason I cant get the focus to work... it clears the field, but moves to the next one....

      Code:
       function isNumeric(string, field, wipeField)
      	 {
      		
      		re = /^\d+(\.\d+)?$/;
      		
      		if (re.test(string))
      		{
      			return true;
      		}
      		else
      		{
      			alert("Please enter only numbers in this field\n\nEg: 32.4");
      			
      			field.value = "";
      			document.event.cancelBubble = true;
      			field.focus();
      			return false;
      			
      		}
      		
      		
      	 }
      I use an onchange code like

      onChange="isNumeric(this.value,this,false);"

      I've also tried onChange="return isNumeric(this.value,this,false);"

      but that doesnt work either..... thanks for any help....
      www.mattfacer.com

      Comment


      • #4
        Remove the cancelBubble line and use

        onchange="return isNumeric(this.value,this,false)"

        That works but behaves weird in Moz. After the alert is displayed, the cursor seems to have focused back to the field (you can see the blinking cursor) but you can't type something in unless you click inside the field. So in reality, the field does not have focus.
        Glenn
        vBulletin Mods That Rock!

        Comment


        • #5
          I use the below onkeyup but you could also use it onchange.
          Code:
          <script type="text/javascript">
           <!--//
            function isNumber(field){
             var regex = /^[0-9+\.]*$/;
              if(!regex.test(field.value)){
                 alert('Please enter only numbers in this field\n\nEg: 32.4');
                 field.value = field.value.replace(/[^0-9+\.]/g,"");
              }
            }
           //-->
          </script>
          </head>
          
          <body>
          <form>
          <input type="text" onkeyup="isNumber(this)">
          </form>
          .....Willy

          Edit: Added your alert.
          Last edited by Willy Duitt; Mar 10, 2004, 06:37 AM.

          Comment


          • #6
            See if this will help

            <script type="text/javascript">

            function numbersonly(myfield, e, dec)

            {

            var key;

            var keychar;



            if (window.event)

            key = window.event.keyCode;

            else if (e)

            key = e.which;

            else

            return true;

            keychar = String.fromCharCode(key);

            keychar = keychar.toUpperCase();



            // control keys

            if ((key==null) || (key==0) || (key==8) ||

            (key==9) || (key==13) || (key==27) )

            return true;



            // numbers

            else if ((("0123456789").indexOf(keychar) > -1))

            return true;



            // decimal point jump

            else if (dec && (keychar == "."))

            {

            myfield.form.elements[dec].focus();

            return false;

            }

            else

            return false;

            }

            </script>
            Beyond a critical point within a finite space, freedom diminishes as numbers increase. ...The human question is not how many can possibly survive within the system, but what kind of existence is possible for those who do survive."

            Comment


            • #7
              That's too long compared to using regular expression which was already used in the above posts.
              Glenn
              vBulletin Mods That Rock!

              Comment


              • #8
                thanks for all the ideas guys!!

                glenngv - I removed the cancel event bubble thing - and added the return line - it now works perfectly!!

                Code:
                function isNumeric(string, field, wipeField)
                	 {
                		
                		re = /^\d+(\.\d+)?$/;
                		
                		if (re.test(string))
                		{
                			return true;
                		}
                		else
                		{
                			alert("Please enter only numbers in this field\n\nEg: 32.4");
                			
                			field.value = "";
                			//document.event.cancelBubble = true;
                			field.focus();
                			return false;
                			
                		}
                		
                		
                	 }
                What I'd like to consider doing now is to get the contents of the field, strip out anything that isnt in the REGEXP thing - and display that... so if the user put

                "34.2%" and moved away from the field - it would leave "34.2"

                any ideas how I could do that?

                thanks everyone for the help!!
                www.mattfacer.com

                Comment


                • #9
                  also - now I have the return being used - it doesnt seem to be calling my other onchange events... this is the code I have one one set of textboxes...

                  onchange="return isNumeric(this.value,this,true);updateTotal(); roundIt(this)"

                  so it does the isNumeric which I have a return true if its ok - but it then doesnt do the other functions....

                  any ideas?
                  www.mattfacer.com

                  Comment


                  • #10
                    Originally posted by homerUK
                    What I'd like to consider doing now is to get the contents of the field, strip out anything that isnt in the REGEXP thing - and display that... so if the user put

                    "34.2%" and moved away from the field - it would leave "34.2"

                    any ideas how I could do that?
                    If you would have run the script I posted.
                    You would have seen that it does exactly what you are asking.

                    .....Willy

                    Comment


                    • #11
                      sheesh--sorry dude... I didnt add that last line in! have now and it works a treat!

                      thanks!!

                      any ideas about why my other scripts arnt working? its to do with the return part on the return isNumeric....
                      www.mattfacer.com

                      Comment


                      • #12
                        All other statements after the return statement will not be executed.

                        Usually you would fix your problem this way;

                        onchange="updateTotal(); roundIt(this); return isNumeric(this.value,this,true)"

                        but that wouldn't make sense since you first need to check if the value is numeric before update or rounding is done.

                        So, you should do it this way:

                        onchange="if (isNumeric(this.value,this,true)) {updateTotal(); roundIt(this)} else return false"

                        or: (better)

                        onchange="return checkAndUpdate(this,true)"

                        then in the script:
                        Code:
                        function checkAndUpdate(obj, wipe){
                          if (isNumeric(obj.value,obj,wipe)) {
                             updateTotal(); 
                             roundIt(obj);
                             return true;
                         }
                         return false;
                        }
                        Glenn
                        vBulletin Mods That Rock!

                        Comment

                        Working...
                        X