tags:

views:

5925

answers:

5

I want to validate my extjs form. When the text field is empty, there should display an alert box and after that our cursor have to focus on the text box. I tried with the coding below. Alert is working fine. but cursor is not focusing. Can you please help me anybody to focus?

if(Ext.get('first').dom.value=='')
{
    Ext.MessageBox.alert('Status', 'Enter your First Name!');
    document.getElementById("first").focus();
}
+3  A: 

The documentation states that MessageBox is asynchronous:

Note that the MessageBox is asynchronous. Unlike a regular JavaScript alert (which will halt browser execution), showing a MessageBox will not cause the code to stop. For this reason, if you have code that should only run after some user feedback from the MessageBox, you must use a callback function (see the function parameter for show for more details).

So, your focus() is carried out immediately after the call to Ext.MessageBox.alert, not when the user dismisses it: When the user clicks to dismiss the alert, the focus will change again.

You could try using a regular javascript alert() instead, which is synchronous.

Paul Dixon
i am not getting your answer. but i tried with reguler js code andExt.get('first').dom.focus() also. But i am not getting correct.
+3  A: 
if (Ext.get("first").dom.value == "") {
    Ext.MessageBox.alert("Status", "Enter your First Name!", function() {
        Ext.get("first").focus();
    });
}
harley.333
A: 

Try this


if(Ext.get('first').dom.value=='')
{
     Ext.Msg.alert('Status', 'Enter your First Name!', function(btn, text){
          if (btn == 'ok'){
               Ext.getCmp('first').focus(true,10);
          }
     });
}
A: 

Ext.getCmp('first').focus(true,10);

Works perfect. Thanks!

Kal
A: 

Thanks harley.

it surely works

rydanzig