Need help to validate an input field to allow for only numbers and decimal

Hi,
I need help to validate an input field to only allow numbers from 0 to 999 and in addition to also allow for up to one decimal place.  

Any help would be appreciated.  

Thanks
-dukiejc
dukiejc99Asked:
Who is Participating?
 
leakim971PluritechnicianCommented:
better : http://jsfiddle.net/nbRYd/8/
function validate(str) {
   return /^\d{0,3}(\.\d)?$/.test(str);
}

function validateAll() {
    var field1 = document.getElementById("field1");
    var field2 = document.getElementById("field2");
    var field3 = document.getElementById("field3");
    var field4 = document.getElementById("field4");
    if( !validate( field1.value ) ) {
        alert("Please enter a correct number");
        field1.focus();
        return false;
    }
    if( !validate( field2.value ) ) {
        alert("Please enter a correct number");
        field2.focus();
        return false;
    }
    if( !validate( field3.value ) ) {
        alert("Please enter a correct number");
        field3.focus();
        return false;
    }
    if( !validate( field4.value ) ) {
        alert("Please enter a correct number");
        field4.focus();
        return false;
    }
    return true;
}

Open in new window

0
 
leakim971PluritechnicianCommented:
function validate(str) {
   return /^\d{0,3}(.\d)?$/.test(str);
}

test page : http://jsfiddle.net/nbRYd/
0
 
darkappleCommented:
Have a look at this jQuery alphanumeric validation plugin
http://www.itgroup.com.ph/alphanumeric/
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
dukiejc99Author Commented:
Sorry but I need a little bit more help on this.  I took a look at both and not sure how to modify the function for the parameters I need.
0
 
leakim971PluritechnicianCommented:
my function receive your string (field value) and return a boolean :
- true if valid
- false if not valid
0
 
leakim971PluritechnicianCommented:
A new test page : http://jsfiddle.net/nbRYd/4/
0
 
dukiejc99Author Commented:
hi leakim971,

Would it be something like this if I have 4 fields to validate?

<script>
function validate(str) {
   return /^\d{0,3}(.\d)?$/.test(str);
}

function validateAll() {
    var field1 = document.getElementById("field1");
    var field2 = document.getElementById("field2");
    var field3 = document.getElementById("field3");
    var field4 = document.getElementById("field4");
          if( !validate( field1.value ) ) {
              alert("Please enter a correct field1 number");
          }
          return true;
         else if(!validate( tc_field2.value )) {
                    alert("Please enter a correct field2 number");
                     }
       return true;
        else if(!validate( field3.value )) {
                    alert("Please enter a correct field3 number");
                    }
       return true;
        else if(!validate( tc_field4.value )) {
                    alert("Please enter a correct field4 number");
                 }
       return true;
}
</script>

<form id="testform" method="post" action="#" onsubmit="return validateAll();">
<input id="field1" class="text" type="text" name="field1" />
<input id="field2" class="text" type="text" name="field2" />
<input id="field3" class="text" type="text" name="field3" />
<input id="field4" class="text" type="text" name="field4" />
<input type="submit" value="Submit">
</form>
0
 
leakim971PluritechnicianCommented:
code updated : http://jsfiddle.net/nbRYd/7/
function validate(str) {
   return /^\d{0,3}(.\d)?$/.test(str);
}

function validateAll() {
    var field1 = document.getElementById("field1");
    var field2 = document.getElementById("field2");
    var field3 = document.getElementById("field3");
    var field4 = document.getElementById("field4");
    if( !validate( field1.value ) ) {
        alert("Please enter a correct number");
        field1.focus();
        return false;
    }
    if( !validate( field2.value ) ) {
        alert("Please enter a correct number");
        field2.focus();
        return false;
    }
    if( !validate( field3.value ) ) {
        alert("Please enter a correct number");
        field3.focus();
        return false;
    }
    if( !validate( field4.value ) ) {
        alert("Please enter a correct number");
        field4.focus();
        return false;
    }
    return true;
}

Open in new window

0
 
dukiejc99Author Commented:
one more thing - it's still allowing me to add more than 4 numbers (9999).  I need it to only allow between 0-999 for integers and up to one decimal after that.  (999.9)
0
 
dukiejc99Author Commented:
Thanks for all your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.