Link to home
Start Free TrialLog in
Avatar of rmk
rmk

asked on

blur regex

I'm trying to make sure that the contents of a text box can only be a poistive or negative integer. If not I want to send the focus back to that textbox.

What's wrong with this jquery code which executes for several textboxes? It is allowing non integer values and it sets the focus to the next contron instead of the control with the problem.

        $(document).ready(function () {
            $(".EOWDPRCYQTextBox").blur(function (event) {
                if (this.value.match(/[+,-]{0,1}[0-9]{1,6}/) == null) {
                    alert("bad");
                    this.focus;
                }
            }) // blur
        });   // ready
Avatar of rmk
rmk

ASKER

I finally got the regular expression correct as follows:

        $(document).ready(function () {
            $(".EOWDPRCYQTextBox").blur(function (event) {
                if (this.value.toString().match(/^([+-]{0,1})([0-9]{1,6})$/) == null) {
                    alert("bad");
                    this.focus;
                }
            }) // blur
        });    // ready

But I still can't get the focus to stay on the same control. How do I do that?
ASKER CERTIFIED SOLUTION
Avatar of rmk
rmk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Michel Plungjan
It is quite dangerous to blur, alert, and focus in the same code. Try pasting a wrong value into a field and then click another field. You should use setTimeout to focus so the alert does not trigger the blur of the next field
Avatar of rmk

ASKER

I am very new to jquery. Can I ask you to modify my code to properly include the setTimeout?
If it works for you then perhaps
 No needd to change. Just be aware that the issue exists
Avatar of rmk

ASKER

solved it myself