How can I get input mask to accept null values for phone number data?

The scenario is:

  • Have two fields, mobile, and homephone.  I want to allow either to be null and use an input mask field on my edit and create forms.
  • I found the custom validation where it checks the length but nothing related to allowing null values
  • I am using ASP .Net Core
  • Both fields are defined in my model as strings and strongly typed as [Phone]
Reasoning:
  • Many people today have cell phones, but some have no land line
Thanks in advance

9 Replies 1 reply marked as answer

SN Sevvandhi Nagulan Syncfusion Team October 12, 2020 12:26 PM UTC


Hi Chris, 



Greetings from Syncfusion support. 


We checked the reported requirement. We would like to inform you that, Validation attributes like [Phone] non-null string values. So PhoneNumber attribute cannot accept the empty string or null values. Please refer to the stack overflow for more information.  



Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/PhoneNumberValidation-52609255 


Please check the above information and get back to us if you have any other queries. 


Regards, 
Sevvandhi N 


Marked as answer

CK chris kraft October 17, 2020 06:31 PM UTC

Neither of the fields are required in my model so by virtue of that it should allow null values per the article you provided and it is not


CK chris kraft October 17, 2020 06:33 PM UTC

Validation attributes like [Phone] and [EmailAddress] will check any non-null string values. Because the string type is inherently nullable, empty strings passed to the ModelBinder are read as null, which passes the validation check.

is the first paragraph of the article link


SN Sevvandhi Nagulan Syncfusion Team October 21, 2020 03:14 PM UTC

Hi Chris, 


Sorry for the delay. 

We are facing complexity while validating the reported query in our end.  We need additional two days for validating the issue.  We will update further details within 2 business days(23rd, October 2020). We appreciate your patience until then. 


Regards, 
Sevvandhi N 



CK chris kraft October 26, 2020 10:56 AM UTC

update please


SN Sevvandhi Nagulan Syncfusion Team October 27, 2020 07:39 AM UTC

Hi Chris, 


We confirmed the reported issue " Validation appearing for empty stings" as a bug in our end and the fix will be available in our Novermber 12th , 2020 patch release. You can track the status of the bug in the below feedback link from below.    






Until then we suggest to below workaround solution. 


Provide the placeholder to the component. Please find the sample and code example, 


<ejs-maskedtextbox id="mask" name="value" mask="000-000-0000" ejs-for="@Model.value" placeholder="Enter value"></ejs-maskedtextbox> 
            <div id="errorMessage"> 
                <span asp-validation-for="value"></span> 
            </div> 
            <div id="submitbutton"> 
                <ejs-button id="submitButton" content="Submit"></ejs-button> 
            </div> 


Please find the sample below, 




Regards, 
Sevvandhi N 



SN Sevvandhi Nagulan Syncfusion Team December 28, 2020 11:18 AM UTC

Hi Chris, 

Sorry for the inconvenience. 

We have validated the issue. We have confirmed that the reported requirement as an improvement from our end and this will be included in any one of the upcoming releases.

Regards, 
Sevvandhi N 



CK chris kraft February 7, 2021 05:17 PM UTC

The sample code requires changes within the .cshtml file as well as within the controller.  Because the this follows the MVC and is strongly typed as Phone in my original question I found that the solution provided does not work.  The form fails dues to the ModelState being invalid.  

I feel like the answer provided would work but would require additional coding to skip over the ModelState or remove the check.  Unfortunately, this is only a partial solution to the issue and I feel like it's an incomplete answer.  I confirmed that the provided code for download does not contain a model file for the project but instead does everything in the controller.

The solution provided for the controller portion loads a blank space for the input mask which causes the ModelState to fail because of the Phone type.

All the information was provided in the initial question i.e. Model, strong type as phone, etc and the response didn't really account for that.

I can fix the problem by adding code checking the affected fields to see if they contain a blank space and convert them to null.  In my opinion, this would have been a better option from the start rather then the one provided.

Since this is a confirmed issue, is there a timeline for an update with a fix?


SN Sevvandhi Nagulan Syncfusion Team February 8, 2021 01:28 PM UTC

Hi Chris, 



Thank you for sharing the details. We checked the reported issue in “ModelState.IsValid in MVC”. When we enter the null or empty value, the ModelState.IsValid being true and returns false if you enter the invalid phone number.  Please refer the below sample and video. 




Please find the sample below. 






Please check the above sample and revert to us if we misunderstood your query or if we missed any case. When implementing the feature, it would be helpful to include that case. We will also be updating the timeline based on the details. 


Regards, 
Sevvandhi N 


Loader.
Up arrow icon