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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

test page : http://jsfiddle.net/nbRYd/
darkappleCommented:
Have a look at this jQuery alphanumeric validation plugin
http://www.itgroup.com.ph/alphanumeric/
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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

leakim971PluritechnicianCommented:
my function receive your string (field value) and return a boolean :
- true if valid
- false if not valid
leakim971PluritechnicianCommented:
A new test page : http://jsfiddle.net/nbRYd/4/
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>
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

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)
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dukiejc99Author Commented:
Thanks for all your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.