[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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
0
dukiejc99
Asked:
dukiejc99
  • 5
  • 4
1 Solution
 
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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
 
dukiejc99Author Commented:
Thanks for all your help.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now