0

I have two textboxes (input fields) like following:

 <input type="text" class="txtmin" placeholder="Minimum price">

            <input type="text" class="txtmax" placeholder="Maximum price">

I'm trying a method like this to prevent the user from entering the following values:

  1. If the user hasn't entered anything in either of these two values, he can proceed.

  2. If the user entered something into txtmin textbox, but hasn't entered anything into the txt max, only txtmin shall be validated

  3. Like 2 but vice versa

  4. 2 and 3 validation occurs on following basis:

    • If user enters anything besides an integer number or floating point number, error is displayed.

So for example when I enter: 1 or 2.50 or 1.00 this is okay...

Number which has comma in it can't be allowed.

So I've tried it like following:

$(".btnGo").click(function () {
             var r = new RegExp("/^[+-]?\d+(\.\d+)?$/");
             var min = $('.txtmin').val();
             var max =$('.txtmax').val(); 
            if ($('.txtSearch').val() == "") {
                ShowMessage("All fields are required");
                return;
            } else if ($('.txtmin').val() != "") {
                if (!r.test(min)) {
                    ShowMessage("Entry only number for minimum price!");
                    return;
                }
            }
            else {
                PostAndUpdate($('.txtSearch').val(), $('input[name=type]:checked').val(), $('input[name=shipping]:checked').val(), $('input[name=condition]:checked').val(), $('.txtmin').val(), $('.txtmax').val());
            }
        });

But for example when I don't leave leave the txtmin value empty, I enter:

1.25 <<<

I still get the error for some reason???

Entry only number for minimum price

What am I doing wrong here?

1 Answer 1

1

Take the quotes out of your regex.

var r = new RegExp(/^[+-]?\d+(\.\d+)?$/);

Follow-up: You can either declare a regex delimited by /.../ or quotes, but not both. If you use quotes, you need to follow normal javascript escape rules and use double backslashes. You also don't need to use new RegExp if using slash delimited. These are all valid and equivalent.

var r = new RegExp(/^[+-]?\d+(\.\d+)?$/);
var r = new RegExp("^[+-]?\\d+(\\.\\d+)?$");
var r = /^[+-]?\d+(\.\d+)?$/;

And no, this will not allow numbers with commas.

Sign up to request clarification or add additional context in comments.

4 Comments

oh goodness you gotta be kidding me mate, only this was the issue? Thanks !!! :) P.S. Does this regex meets the criteria that i mentioned above, like for example it won't allow numbers with comma?
sounds good. P.S. How can I expand this regex so that the field isn't required to have value, but if it does, i'd validate it according to the regex (without using extra if statement)
Use \d* (zero or many times) instead of \d+ (one or many times) - ^[+-]?\d*(\.\d+)?$
would this apply to the html validation as well? Like for example putting the pattern tag onto the textbox and then: pattern="^[+-]?\d*(\.\d+)?$" ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.