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

get first char in RadMaskedTextBox onkeypress event

2 Answers 124 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Rhbkv
Top achievements
Rank 1
Rhbkv asked on 14 Mar 2012, 12:30 AM

We are using "RadInput.Net2.dll" and RadMaskedTextBox for phone number. We want to check if first character of area code should not be 0 or 1.

If we use CustomValidator and javascript as below, it is working fine.
For example, if phone # is "(316) 446-2777" and if I replace 3 as 1, I could see the correct error message. But if I delete 3, I can see numbers in text box as "(_16) 446-2777". But still I can see the error message as "This number cannot begin with a 1 or 0.". The prompt character is not part of the args.value.

I want to display the error message only if first character after ( is 0 or 1. Could you please help us to find out the first character only of the input text
Thanks in advance.

<radI:RadMaskedTextBox ID="txtPhone" runat="server">
             </radI:RadMaskedTextBox><br />          
              
            <asp:CustomValidator id="revBusinessPhone" Display="dynamic" runat = "server" ControlToValidate="txtPhone"  ErrorMessage = "This number cannot begin with a 1 or 0." ClientValidationFunction ="validateLength"  ></asp:CustomValidator>
  
  
  
   function validateLength(oSrc, args) {
  
         var phnVal = args.Value;
        
         if (((phnVal1.substring(0, 2)) == "(1" || (phnVal.substring(0, 2) == "(0"))) {
             oSrc.innerText = "This number cannot begin with a 1 or 0.";
              args.IsValid = false;
  
         }
         else {
         var regex = /^(\([0-9]\d{2}\)|[0-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/;
            if (regex.test(phnVal)) {
                  args.IsValid = true;
             }
             else {
                 oSrc.innerText = "This number must contain 10 digits.";
                  args.IsValid = false;
             }
         }
         return true;
     }

2 Answers, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 15 Mar 2012, 09:14 AM
Hello Rhbkv,

By design the RadMaskedTextBox uses plain value for the validation field. It is like "164462777" in your case.
If you like to get the formatted value you could use this code:
var phnVal1  = $find("txtPhone").get_displayValue();
This will get the value the same way as it looks to the user.

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
Rhbkv
Top achievements
Rank 1
answered on 15 Mar 2012, 06:20 PM
Thank you very much Vasil!
Tags
General Discussions
Asked by
Rhbkv
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Rhbkv
Top achievements
Rank 1
Share this question
or