Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to validate mobile number if mobile number is greater than 10 it give error or alert here is my code:
C#
function check()
     {
var pattern=/^[A-Za-z0-9]{10}$/;

     var regexp = new RegExp(pattern);
     //alert(document.getElementById("ctl00_ContentPlaceHolder1_txtPhoneNumber").value);
     if(regexp.test(document.getElementById("ctl00_ctl00_ContentPlaceHolder1_ContentProfile_txtMobileNumber").value))
     {
        return true;
     }
     else
     {

      alert('Phone is invalid');
     return false;
     }
     }

and calling this function to custom validator
<asp:CustomValidator ID="CVtxtPhoneNumber" ClientValidationFunction="check();" ControlToValidate="txtMobileNumber" runat="server" ErrorMessage="Enter a valid Phone No."  ValidationGroup="EditProfile" SetFocusOnError="True" Display="Dynamic"></asp:CustomValidator>

The problem is that alert is displayed but i want the only the error message is displayed "Enter a valid phone no.". not the alert
Posted
Updated 14-Dec-10 23:08pm
v2
Comments
Abdul Quader Mamun 15-Dec-10 5:07am    
Use pre tag for better reading.
Toniyo Jackson 15-Dec-10 5:08am    
Always write your code inside code block

1 solution

First you need to have two arguments sender and args
as check(sender,args) and you need to set args.IsValid = false; when you want to show error message. Rest thing will be take care by validator itself.

your check function would be as

function check(sender,args)
        {
        var pattern=/^[A-Za-z0-9]{10}$/;
            var regexp = new RegExp(pattern);
                //alert(document.getElementById("ctl00_ContentPlaceHolder1_txtPhoneNumber").value);
                 if(regexp.test(document.getElementById("ctl00_ctl00_ContentPlaceHolder1_ContentProfile_txtMobileNumber").value))
                 {
                    args.IsValid = true;
                 }
                 else
                 {
                    args.IsValid = false;
                 }
            }
 
Share this answer
 
v2
Comments
balongi 15-Dec-10 5:58am    
i want to show error message when it not match with regular expression. where i make change please tell
Brij 15-Dec-10 6:07am    
you need to change the decalaration of your function as check(sender,args) and set args.IsValid = false;, where you showing alert. I added the check method in my answer
balongi 15-Dec-10 6:20am    
after make change, the eror messege is not displyed:
here is changed code:
function check(sender, args)
{

var pattern=/^[A-Za-z0-9]{10}$/;

var regexp = new RegExp(pattern);
//alert(document.getElementById("ctl00_ContentPlaceHolder1_txtPhoneNumber").value);
if(regexp.test(document.getElementById("ctl00_ctl00_ContentPlaceHolder1_ContentProfile_txtMobileNumber").value))
{
return true;
}
else
{

args.IsValid = false;


}
}
balongi 15-Dec-10 6:46am    
If i add function as check(sender,args) , the syntex error is come
balongi 15-Dec-10 7:01am    
after your suggestion i added code, now my textbox is accepting all kind of text where it string or digit. Now Error messeage is not displayng

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900