We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

add validation to check for valid telephone number

Hi Guys happy new year,
Could you tell me how to add validation in MVC 5 grid controll,
addrule to validate telephone number or cell number

Thanks

Edmund Herbert

3 Replies

PK Prasanna Kumar Viswanathan Syncfusion Team January 5, 2017 11:37 AM UTC

Hi Edmund, 

Thanks for contacting Syncfusion support. 

To add validation in grid control, use validationRules property of columns. If you have different field for both telephone and mobile number, use minlength and maxlength jQuery validation method. For example, in this sample we set validation for phone number using maxlength and minlength validation method.  

Find the code example and sample:  


@(Html.EJ().Grid<object>("FlatGrid") 
    .Datasource((IEnumerable<object>)ViewBag.datasource) 
    .AllowSorting() 
         ---------- 
     }) 
       .Columns(col => 
        { 
          --------------- 
            col.Field("Phone").HeaderText("Phone").ValidationRules(v => v.AddRule("required", true).AddRule("maxlength", 6).AddRule("minlength", 6)).TextAlign(TextAlign.Right).Width(75).Add(); 
        }) 
    ) 


Refer to the Help document for the validation.  


If you need to validate phone and mobile number in a single field, use custom validation. In addition to jQuery validation methods, you can also add your own custom validation methods for a specific column. Function call to custom validator function to be mentioned within ValidationRules property of Columns. 
 
For more information, please refer the below document and online sample.  
 
 

Regards, 
Prasanna Kumar N.S.V 



OD oddssatisfy November 20, 2022 12:46 AM UTC

@Edmund Herbert

Hello. Is there a way to validate a phone number? I have been doing the ff_valuenotempty, but it will accept anything someone puts there. Is there a way to see if its a number?

I think the easiest way would be to check that it is the appropriate number of digits, with no spaces, but I understand the integer validation only checks to see if its an integer, not one of a specific length.

Any thoughts?



PS Pon Selva Jeganathan Syncfusion Team November 22, 2022 05:17 AM UTC

Hi oddssatisfy,


Query: Is there a way to validate a phone number? I have been doing the ff_valuenotempty, but it will accept anything someone puts there. Is there a way to see if its a number?


You can validate the phone number using the below code snippet,


@(Html.EJ().Grid<object>("HierarchyGrid")

 

                     ….

                    .Columns(col =>

                    {

                        …

                        col.Field("PhoneNumber").HeaderText("Phone number").Width(110).ValidationRules(v => v.AddRule("required", true).AddRule("digits", true).AddRule("maxlength", 10).AddRule("minlength", 10)).Add();

                    })


In the above code snippet, we used the digits validation rule and the maxlength and minlength validation rules. When using the digits validation rule, you can only enter the digit .


Please refer to the below sample,

https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication1_-_EJ1_MVC573825102


Please refer to the below help documentation,

https://help.syncfusion.com/aspnetmvc/grid/editing?cs-save-lang=1&cs-lang=razor#column-validation

https://jqueryvalidation.org/digits-method/


And you can also add your own custom validation methods for a specific column. Function call to custom validator function to be mentioned within ValidationRules  property of Columns.


Please refer the below document and online sample. 


https://help.syncfusion.com/aspnetmvc/grid/editing#custom-validation


http://mvc.syncfusion.com/demos/web/grid/customvalidation 


Regards,

Pon selva


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Loader.
Live Chat Icon For mobile
Up arrow icon