We help IT Professionals succeed at work.

blur regex

rmk
rmk asked
on
Medium Priority
518 Views
Last Modified: 2013-11-17
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
Comment
Watch Question

rmk

Author

Commented:
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?
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
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
rmk

Author

Commented:
I am very new to jquery. Can I ask you to modify my code to properly include the setTimeout?
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
If it works for you then perhaps
 No needd to change. Just be aware that the issue exists
rmk

Author

Commented:
solved it myself
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.