0

I need to validate a text based upon a regular expression in javascript. My regular expression is working fine but but can't find out why it is not working in java script.

The regular expression is this , ^([-+/*]\d+(\.\d+)?)*

Valid expressions are +7 or +9.36*8 or +4-9.3/5.0

Invalid matches are test or 8.36

Here is the code,

    var ck_diffrentialformula = /^([-+/*]\d+(\.\d+)?)*/;

            function radtxtbxLinkedDifferentialFormulaOnBlur(sender, eventArgs) {
                if (sender.get_value().length > 0) {

                    var entered_value = sender.get_value();

                    if (ck_diffrentialformula.test(entered_value)) {
                     alert('Text Matches');
                    }
                    else {
                     alert('Text does not match');
                    }
                }
            }

sender.get_value() - gives the text box value over here.

Please tell me where I am doing wrong.

19
  • are u getting any of the alerts? Commented Jul 6, 2012 at 6:39
  • Please provide an example of the actual data that needs to be validated (both a valid and an invalid example would be best). Commented Jul 6, 2012 at 6:39
  • 1
    It would help us to know the value of sender.get_value() immensely (and what you expect the alert to be). Commented Jul 6, 2012 at 6:39
  • @Sirwani - Yes, but even if I enter the correct format it gives me error message. Commented Jul 6, 2012 at 6:40
  • 1
    Your code, exactly as it is, works with absolutely no problems and matches all your test data: jsbin.com/etigug/edit#source Commented Jul 6, 2012 at 6:54

2 Answers 2

2

I'm assuming you're using it something like this:

<input type="text" onchange="radtxtbxLinkedDifferentialFormulaOnBlur(this,null)" />

sender has no method .get_value() so that won't even work. Replacing that, it seems to work.

function radtxtbxLinkedDifferentialFormulaOnBlur(sender, eventArgs) {
    console.log(sender)
    if (sender.value.length > 0) {
        console.log(sender.value);
        if (ck_diffrentialformula.test(sender.value)) {
         console.log('Text Matches');
        }
        else {
         console.log('Text does not match');
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Updated my question, I am getting the textbox value using 'sender.get_value()' and the value is also coming
So sender.getValue() returns the correct value... all you have left is an if statement. Syntax is valid. Something else is wrong. Show us the getvalue code
sender.getValue() returns the correct value i have checked it using an alert box
can you alert entered_value?
If you can get that far, it should alert something because you have an if and an else... something else is causing this. youll have to share the whole code. I honestly don't understand. your code works as is.
1

Maybe you want to try this expression: /^([\-+\/*]\d+(\.\d+)?)*$/
It escapes "-" and "/" (jsbin suggests it) within the regex and considers the whole string ($ at the end).

1 Comment

Thanks, it works just I had to add an extra escape character. The correct regular expression is /^([\-+\/\*]\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.