suchislife
suchislife

Reputation: 1

How to check if a string is a positive integer not starting with 0 using regex?

After much research I have yet to discover a regular expression that satisfies all of these IF conditions. The code works great but I think it can be streamlined with RegEx.

The goal: Return true on positive integers ranging from 1 through 9 NOT starting with a zero. Return false for every single other value.

Take a look.

// Validation functions


function hasPeriod(s) {
  // Return true or false.
  // True if a period is entered at any point in time.
  return s.indexOf('.') > -1;
}
// See more... http://2ality.com/2014/05/is-integer.html
function isInteger(s) {
  // Return true or false.
  // True if the remainder of dividing it by 1 is 0.
  return s % 1 === 0;
}



// Bind keyup event to prep_work_item_input.
$('#input1').on('keyup', function (e) {

  // TRIM text input value on declaration...
  // May contain spaces...
  var input_value = $(this).val().trim();

  // Refresh text input after Trim!
  // Does not contain spaces
  $(this).val(input_value);

  // If text input is EMPTY after Trim...
  if (input_value.length == 0) {

    // Stop at this if and dont proceed.
    return false;

  } 

  // If a PERIOD is typed into text input at any point in time...
  if (hasPeriod(input_value)) {

    // Clear input
    $(this).val('');

    // Stop at this if and dont proceed.
    return false;

  }

  // If text input value is NOT an INTEGER...
  if (!isInteger(input_value)) {

    // Clear input
    $(this).val('');

    // Stop at this if and dont proceed.
    return false;
  }

  // If text input value IS an INTEGER but EQUAL to 0...
  if (input_value == 0) {

    // Clear input
    $(this).val('');

    // Stop at this if and dont proceed.
    return false;
  }

  // If text input value IS an INTEGER and also GREATER than 0...
  if (input_value > 0) {

    // If we have made this far... 
    // we probably have a real integer that is not zero.

    // We can finally begin to calculate

    input_value = parseInt(input_value);

    // random cost multiplier for this example
    var cost = parseFloat('0.78');

    // calculate subtotal...
    cost = input_value * cost;

    // Set prep_work_item_subtotal...        
    console.log(input_value + ' ' + 'SF' + ' / ' + ' $' + cost.toFixed(2));

  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<label>Enter value below:</label>
<br>
<input type="text" id="input1" placeholder="enter value">

Upvotes: 1

Views: 1166

Answers (2)

mx0
mx0

Reputation: 7123

From reading your code it looks like you need to check if a number is grater > 0.
You can do this with this regex:

^[1-9]\d*$

It will check that input:
- starts with a digit but not 0
- contains any number of digits
- does end with a digit

Upvotes: 4

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Regex is not the solution to what you are trying to do.

What you should do instead is provide meaningful error messages to the user based on exactly which if statement failed.

For instance, if your input requires a . but there is none, you can then tell the user exactly what you want from them. But if you had just one big über-regex, you would have absolutely no way of knowing what is causing it to fail.

Consider this a little UX lesson!


Or just use the correct input type.

<input type="number" min="0.01" step="0.01" />

Upvotes: 2

Related Questions