views:

47

answers:

2
$("#htxtEmoney, #htxtPoint").keypress(function(e){
    var evt = window.event ? window.event : e;  //for compatibility
    var keyCode = evt.keyCode ? evt.keyCode : e.which;
    if (keyCode == 13 || keyCode == 27 || keyCode == 8) return ; // esc, enter
    if (48 <= keyCode && keyCode <= 57)
        return true; // number

    alert("You can write only Number.");

    this.focus();
    return false;
}).blur(function(e){
    Payments.onChangePoint(this);
});

Here is a code I wrote.

If the user input alphabet, the alert() occur.

and then the blur() function also occur.

But I want, If an user input alphabet, the alert() occur and then the blur() function doesn't occur at that situation.

I mean the blur() fuction occur only the user click other control..

sorry about my poor English. thx.

+4  A: 

An alert shifts focus away from anything that has it, therefore automatically causing a blur. You cannot have an alert that will not cause a blur to occur.

A better solution to your problem would be to not cause an alert but rather to display a message next to or underneath your input. This will inform the user of the error and not lose focus on the input.

Darko Z
thank you. I will show msg-div under the input. I think it is the best way.
sunglim
A: 

You could add a variable that both functions have access to, outside of either, like

var izError = false;

and set it to true whenever you display the alert (izError = true;).

izError = false; //reset variable to false

....... //all your intervening code

izError = true;
alert("Don't do that.");

And then test for it before doing your onblur stuff:

if ( izError == false) {
  Payments.onChangePoint(this);
}
D_N
thank you. your solution is also good solution.
sunglim