[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

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
0
rmk
Asked:
rmk
  • 4
  • 2
1 Solution
 
rmkAuthor 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?
0
 
rmkAuthor Commented:
Problem solved as follows:

        $(document).ready(function () {
            $(".EOWDPRCYQTextBox").blur(function (event) {
                if (this.value.toString().match(/^([+-]{0,1})([0-9]{1,6})$/) == null) {
                    var id = $(this).attr("id");
                    alert("bad" + "\n" + id);
                    $("#" + id).focus();
                }
            }) // blur
        });     // ready
0
 
Michel PlungjanIT ExpertCommented:
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
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
rmkAuthor Commented:
I am very new to jquery. Can I ask you to modify my code to properly include the setTimeout?
0
 
Michel PlungjanIT ExpertCommented:
If it works for you then perhaps
 No needd to change. Just be aware that the issue exists
0
 
rmkAuthor Commented:
solved it myself
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now