13design
13design

Reputation: 31

Finding specific values in a string of numbers

I am having trouble trying to validate an interest rate. The conditions are as follows: ( Must be present. Must be numeric. Allowable values: 3.000 thru 16.000 inclusive.).

I am having trouble trying to determine if after the 1st digit is a number, if the next is also one. ie "2N34" should come up as an error. Everytime i tried to use a for loop it just doesn't work. I am wondering if someone can point me in the right direction to test this and submit the proper error.

JAVASCRIPT FUNCTION:

function valIntRate(errMessages){

var rate = document.getElementById("intRate").value;
if (rate.length === 0){
    errMessages += "Interest rate can't be left empty";
}


if (rate < 3 || rate > 16){
        errMessages += "Rate must be between 3 and 16 inclusive";
}

return errMessages;
}

HTML (where the input field is) :

  <label class="label"> Interest  Rate </label>

      <input type="text" name="intRate" id="intRate" size="7" maxlength="6" >

Upvotes: 0

Views: 47

Answers (3)

Shiran Dror
Shiran Dror

Reputation: 1480

you can use parseFloat like this

function valIntRate(errMessages) {

  var val = document.getElementById("intRate").value
  var rate = parseFloat(val);
  if (rate.toString() != val) 
  // any letters will be removed so if there is anything not number or dot this will be true
  {
    errMessages += "Must be a number";
  }
  if (rate.length === 0) {
    errMessages += "Interest rate can't be left empty";
  }


  if (rate < 3 || rate > 16) {
    errMessages += "Rate must be between 3 and 16 inclusive";
  }

  return errMessages;
}

Upvotes: 0

Mark
Mark

Reputation: 569

You may want to look into regular expressions, which can match strings based on a number of rules. In your case, something like this can validate that your value is only digits:

if (rate.match(/^\d*\.?\d+$/)) {
    // rate only has numbers
} else {
    // rate has other characters
}

Edit:

If you're not allowed to use regular expressions, isNaN, typeof, or other built-in functions for this purpose and have to explicitly loop through character-by-character (is this a school assignment, if so it would be nice if you post your own attempt to solve it first), then you can loop through the string contents and check if it's a digit or decimal. (You'll likely also want to ensure that only one decimal is present.)

Here's something to get you started:

var index, character;
for (index = 0; index < rate.length; index += 1) {

    character = rate[index];
    // Test to see if character is a digit or decimal here

}

Upvotes: 0

Bhuneshwer
Bhuneshwer

Reputation: 587

use isNaN(input); to validate the numeric value. This should work.

function valIntRate(errMessages) {
var rate = document.getElementById("intRate").value;
if (rate.length === 0) {
    errMessages += "Interest rate can't be left empty";
}
if (!(rate >= 3 && rate <= 16)) {
    errMessages += "Rate must be between 3 and 16 inclusive";
}
if (isNaN(rate)) {
    errMessages += "Rate must be  a number";
}
return errMessages;
}

Upvotes: 1

Related Questions