This is a migrated thread and some comments may be shown as answers.

Onblur in maskedTextBox

6 Answers 210 Views
Input
This is a migrated thread and some comments may be shown as answers.
Bruno
Top achievements
Rank 1
Bruno asked on 29 Mar 2012, 01:24 PM
I have a maskedTextbox with 2 events, onblur client side and OnTextChanged in server side.
My on blur method are returning true or false, if my return is true, i wanna enter in server side and do OnTextChanged, if return is false doesnt enter on server side and show a alert. 
My problem is, my return can be true or false, doesnt make anything different, always enter in server side.

function verificarCPF() {
    var contador;
    var valorCPF = $telerik.findMaskedTextBox("txtBuscaCpf").get_value();
    var numeroCPF = valorCPF.substr(0, 9);
    var digitoCPF = valorCPF.substr(9, 2);
    var d1 = 0;
 
    for (contador = 0; contador < 9; contador++) {
        d1 += numeroCPF.charAt(contador) * (10 - contador);
    }
    if (d1 == 0) {
        radalert("CPF Inválido!",350,140,"Mensagem",null,null);
        return false;
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (digitoCPF.charAt(0) != d1) {
        radalert("CPF Inválido!", 350, 140, "Mensagem", null, null);
        return false;
    }
 
    d1 *= 2;
    for (contador = 0; contador < 9; contador++) {
        d1 += numeroCPF.charAt(contador) * (11 - contador);
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (digitoCPF.charAt(1) != d1) {
        radalert("CPF Inválido!", 350, 140, "Mensagem", null, null);
        return false;
    }
    return true;
    //radalert("CPF Válido!", 350, 140, "Mensagem", null, null);
}

<telerik:RadMaskedTextBox ID="txtBuscaCpf" ClientEvents-OnBlur="verificarCPF" AutoPostBack="true" runat="server" Mask="###.###.###-##"
    Width="100" OnTextChanged="txtBuscaCpf_TextChanged" />

Thankz in advance!

6 Answers, 1 is accepted

Sort by
0
Bruno
Top achievements
Rank 1
answered on 29 Mar 2012, 05:41 PM
As i sya, my return can be true or false, ALWAYS return true, and go to server side.
help please!
0
Vasil
Telerik team
answered on 30 Mar 2012, 08:34 AM
Hello Bruno,

The OnBlur can not be cancelled and indeed there is no difference what you will return. As a workaround you could handle the client OnValueChanging event and to cancel it. In the same time also handle the OnBlur and just Focus back the element if needed.

All the best,
Vasil
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Bruno
Top achievements
Rank 1
answered on 30 Mar 2012, 12:58 PM
But i dont wanna cancel the OnBlur event, i wann the onblur work as all javascript should work. if return false, doesnt enter in TextChanged Event, if true, enter. its simple
0
Accepted
Vasil
Telerik team
answered on 30 Mar 2012, 02:23 PM
Hello Bruno,

Then in your code instead of returning false, change the auto post back of your input in order to prevent the PostBack.
$find("txtBuscaCpf").set_autoPostBack(false);


Greetings,
Vasil
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Bruno
Top achievements
Rank 1
answered on 30 Mar 2012, 02:45 PM
Thankz bro, this code works perfectly!

But can you explain, why the false return doesnt work? Because, before i start to work with telerik, i was using the asp.net controls, in then, when i was need to do something like that, i do something like OnClientClick"If method() != true; return false" < just example.
In telerik, i try that without success, and the return just false, doesnt work. I will always have to set postback false, when i wanna a behavior like this ?
0
Vasil
Telerik team
answered on 02 Apr 2012, 08:17 AM
Hi Bruno,

There several reasons that we took this design decision for our client events.

With "return false" you could prevent the default action of the HTML DOM Event. However the client side events of RadControls are not HTML DOM Events.
In the event that can be canceled we are passing arguments that inherit Sys.CancelEventArgs clss, that supports the set_cancel function.
Then in our code we are checking if the event is canceled, and we are not checking what is the return value of the handler. This is more flexible design, when it comes to write a complex solution. And here are few examples:

The code in the handler after canceling it will still be executed if you like:
function example1Handler(sender, eventArgs)
{
       //...
       //perform some logic here
       //...
 
        eventArgs.set_cancel(true);
 
       //...
       //code in this section will still be executed
 
        return;
 
       //... use return if you want to return your function, regardless if you want to cancel the event
       //code after the return will not be executed.
}

You can reconsider your decision of canceling the event.
function example2handler(sender, args)
{
      if (some_condition)
      {
           args.set_cancel(true);
      }
      //perform some more logic here
     if (yet_another_condition)
     {
           args.set_cancel(false);
     }
}

You can pass the args to other functions and easily track and change the canceling of the event:
function example3handler(sender, args)
{
      if (some_condition)
      {
         example1handler(sender, args);
      }
      else
      {
         example2handler(sender, args);
      }
     if (another_condition || args.get_cancel())
     {
         args.set_cancel(false);        
     }
}


Greetings,
Vasil
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Input
Asked by
Bruno
Top achievements
Rank 1
Answers by
Bruno
Top achievements
Rank 1
Vasil
Telerik team
Share this question
or