Custom number validation not working properly when value isn't a number










-1















I've got custom validation that checks another field, and then determines if the field in question is within a certain number range. It works fine whenever the user enters a number. However, if the user passes in a non-number, this non-number (the value parameter) doesn't get passed to the validation. Instead, null is passed to the validation. I'd rather have the number with the commas be passed in. Instead, the user is presented with a cryptic "The value XXX is not valid for XXX which isn't helpful. I've also got [Required] and [Numeric] data annotations on this field. These usually catch any issues. However, if the user enters a valid number with a comma, the value won't get passed to my custom validation. I'm not sure where the issue is happening. Any thoughts? I've tried stripping out the commas via javascript, but the value being passed to my custom validation is still null. Here's my custom validation that is being called:



public sealed class RequiresRangeAmount : ValidationAttribute

public RequiresRangeAmount(string property)

Property = property;


public string Property get; set;

protected override ValidationResult IsValid(object value, ValidationContext validationContext)

var property = validationContext.ObjectType.GetProperty(Property);

if (property != null)
loanSubType == LoanSubTypeConstants.Visa2)

var valueConverted = Convert.ToDecimal(value);
if (valueConverted < 5000


return null;




Here is is being used:



[Required]
[Numeric]
[RequiresRangeAmount("LoanSubType", ErrorMessage = "A VISA must be in XXX range.")]
[DisplayName("Requested Loan Amount")]
public decimal? RequestedAmount get; set;


Front-end to strip the commas out doesn't help:



$("#mainForm").submit(function() 
var strippedCommasAmount = $("#RequestedAmount").val().replace(/,/g, '');
$("#RequestedAmount").val(strippedCommasAmount);
);









share|improve this question
























  • Please show us a complete example and the error message you are receiving.

    – Jasen
    Nov 14 '18 at 17:44











  • I'm not sure what else you want? When I include a comma, the custom validation reads the value passed in as null. When I don't include a comma, the value successfully passes into the custom validation.

    – The Vanilla Thrilla
    Nov 14 '18 at 18:39











  • The comma would make the value a string then you would have binding issues.

    – Jasen
    Nov 14 '18 at 19:14











  • But I even tried stripping the commas out on the front-end and this didn't resolve it. I've added that code to show.

    – The Vanilla Thrilla
    Nov 14 '18 at 19:16











  • what if instead of comma, you have a dot?

    – Siegfried.V
    Nov 14 '18 at 20:04















-1















I've got custom validation that checks another field, and then determines if the field in question is within a certain number range. It works fine whenever the user enters a number. However, if the user passes in a non-number, this non-number (the value parameter) doesn't get passed to the validation. Instead, null is passed to the validation. I'd rather have the number with the commas be passed in. Instead, the user is presented with a cryptic "The value XXX is not valid for XXX which isn't helpful. I've also got [Required] and [Numeric] data annotations on this field. These usually catch any issues. However, if the user enters a valid number with a comma, the value won't get passed to my custom validation. I'm not sure where the issue is happening. Any thoughts? I've tried stripping out the commas via javascript, but the value being passed to my custom validation is still null. Here's my custom validation that is being called:



public sealed class RequiresRangeAmount : ValidationAttribute

public RequiresRangeAmount(string property)

Property = property;


public string Property get; set;

protected override ValidationResult IsValid(object value, ValidationContext validationContext)

var property = validationContext.ObjectType.GetProperty(Property);

if (property != null)
loanSubType == LoanSubTypeConstants.Visa2)

var valueConverted = Convert.ToDecimal(value);
if (valueConverted < 5000


return null;




Here is is being used:



[Required]
[Numeric]
[RequiresRangeAmount("LoanSubType", ErrorMessage = "A VISA must be in XXX range.")]
[DisplayName("Requested Loan Amount")]
public decimal? RequestedAmount get; set;


Front-end to strip the commas out doesn't help:



$("#mainForm").submit(function() 
var strippedCommasAmount = $("#RequestedAmount").val().replace(/,/g, '');
$("#RequestedAmount").val(strippedCommasAmount);
);









share|improve this question
























  • Please show us a complete example and the error message you are receiving.

    – Jasen
    Nov 14 '18 at 17:44











  • I'm not sure what else you want? When I include a comma, the custom validation reads the value passed in as null. When I don't include a comma, the value successfully passes into the custom validation.

    – The Vanilla Thrilla
    Nov 14 '18 at 18:39











  • The comma would make the value a string then you would have binding issues.

    – Jasen
    Nov 14 '18 at 19:14











  • But I even tried stripping the commas out on the front-end and this didn't resolve it. I've added that code to show.

    – The Vanilla Thrilla
    Nov 14 '18 at 19:16











  • what if instead of comma, you have a dot?

    – Siegfried.V
    Nov 14 '18 at 20:04













-1












-1








-1








I've got custom validation that checks another field, and then determines if the field in question is within a certain number range. It works fine whenever the user enters a number. However, if the user passes in a non-number, this non-number (the value parameter) doesn't get passed to the validation. Instead, null is passed to the validation. I'd rather have the number with the commas be passed in. Instead, the user is presented with a cryptic "The value XXX is not valid for XXX which isn't helpful. I've also got [Required] and [Numeric] data annotations on this field. These usually catch any issues. However, if the user enters a valid number with a comma, the value won't get passed to my custom validation. I'm not sure where the issue is happening. Any thoughts? I've tried stripping out the commas via javascript, but the value being passed to my custom validation is still null. Here's my custom validation that is being called:



public sealed class RequiresRangeAmount : ValidationAttribute

public RequiresRangeAmount(string property)

Property = property;


public string Property get; set;

protected override ValidationResult IsValid(object value, ValidationContext validationContext)

var property = validationContext.ObjectType.GetProperty(Property);

if (property != null)
loanSubType == LoanSubTypeConstants.Visa2)

var valueConverted = Convert.ToDecimal(value);
if (valueConverted < 5000


return null;




Here is is being used:



[Required]
[Numeric]
[RequiresRangeAmount("LoanSubType", ErrorMessage = "A VISA must be in XXX range.")]
[DisplayName("Requested Loan Amount")]
public decimal? RequestedAmount get; set;


Front-end to strip the commas out doesn't help:



$("#mainForm").submit(function() 
var strippedCommasAmount = $("#RequestedAmount").val().replace(/,/g, '');
$("#RequestedAmount").val(strippedCommasAmount);
);









share|improve this question
















I've got custom validation that checks another field, and then determines if the field in question is within a certain number range. It works fine whenever the user enters a number. However, if the user passes in a non-number, this non-number (the value parameter) doesn't get passed to the validation. Instead, null is passed to the validation. I'd rather have the number with the commas be passed in. Instead, the user is presented with a cryptic "The value XXX is not valid for XXX which isn't helpful. I've also got [Required] and [Numeric] data annotations on this field. These usually catch any issues. However, if the user enters a valid number with a comma, the value won't get passed to my custom validation. I'm not sure where the issue is happening. Any thoughts? I've tried stripping out the commas via javascript, but the value being passed to my custom validation is still null. Here's my custom validation that is being called:



public sealed class RequiresRangeAmount : ValidationAttribute

public RequiresRangeAmount(string property)

Property = property;


public string Property get; set;

protected override ValidationResult IsValid(object value, ValidationContext validationContext)

var property = validationContext.ObjectType.GetProperty(Property);

if (property != null)
loanSubType == LoanSubTypeConstants.Visa2)

var valueConverted = Convert.ToDecimal(value);
if (valueConverted < 5000


return null;




Here is is being used:



[Required]
[Numeric]
[RequiresRangeAmount("LoanSubType", ErrorMessage = "A VISA must be in XXX range.")]
[DisplayName("Requested Loan Amount")]
public decimal? RequestedAmount get; set;


Front-end to strip the commas out doesn't help:



$("#mainForm").submit(function() 
var strippedCommasAmount = $("#RequestedAmount").val().replace(/,/g, '');
$("#RequestedAmount").val(strippedCommasAmount);
);






c# asp.net-mvc validation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 19:41







The Vanilla Thrilla

















asked Nov 14 '18 at 17:14









The Vanilla ThrillaThe Vanilla Thrilla

1,21152444




1,21152444












  • Please show us a complete example and the error message you are receiving.

    – Jasen
    Nov 14 '18 at 17:44











  • I'm not sure what else you want? When I include a comma, the custom validation reads the value passed in as null. When I don't include a comma, the value successfully passes into the custom validation.

    – The Vanilla Thrilla
    Nov 14 '18 at 18:39











  • The comma would make the value a string then you would have binding issues.

    – Jasen
    Nov 14 '18 at 19:14











  • But I even tried stripping the commas out on the front-end and this didn't resolve it. I've added that code to show.

    – The Vanilla Thrilla
    Nov 14 '18 at 19:16











  • what if instead of comma, you have a dot?

    – Siegfried.V
    Nov 14 '18 at 20:04

















  • Please show us a complete example and the error message you are receiving.

    – Jasen
    Nov 14 '18 at 17:44











  • I'm not sure what else you want? When I include a comma, the custom validation reads the value passed in as null. When I don't include a comma, the value successfully passes into the custom validation.

    – The Vanilla Thrilla
    Nov 14 '18 at 18:39











  • The comma would make the value a string then you would have binding issues.

    – Jasen
    Nov 14 '18 at 19:14











  • But I even tried stripping the commas out on the front-end and this didn't resolve it. I've added that code to show.

    – The Vanilla Thrilla
    Nov 14 '18 at 19:16











  • what if instead of comma, you have a dot?

    – Siegfried.V
    Nov 14 '18 at 20:04
















Please show us a complete example and the error message you are receiving.

– Jasen
Nov 14 '18 at 17:44





Please show us a complete example and the error message you are receiving.

– Jasen
Nov 14 '18 at 17:44













I'm not sure what else you want? When I include a comma, the custom validation reads the value passed in as null. When I don't include a comma, the value successfully passes into the custom validation.

– The Vanilla Thrilla
Nov 14 '18 at 18:39





I'm not sure what else you want? When I include a comma, the custom validation reads the value passed in as null. When I don't include a comma, the value successfully passes into the custom validation.

– The Vanilla Thrilla
Nov 14 '18 at 18:39













The comma would make the value a string then you would have binding issues.

– Jasen
Nov 14 '18 at 19:14





The comma would make the value a string then you would have binding issues.

– Jasen
Nov 14 '18 at 19:14













But I even tried stripping the commas out on the front-end and this didn't resolve it. I've added that code to show.

– The Vanilla Thrilla
Nov 14 '18 at 19:16





But I even tried stripping the commas out on the front-end and this didn't resolve it. I've added that code to show.

– The Vanilla Thrilla
Nov 14 '18 at 19:16













what if instead of comma, you have a dot?

– Siegfried.V
Nov 14 '18 at 20:04





what if instead of comma, you have a dot?

– Siegfried.V
Nov 14 '18 at 20:04












1 Answer
1






active

oldest

votes


















0














don't know if it helps, but I have that function I now use in all of my projects :



public static double DoubleParseAdvanced(this string strToParse, char decimalSymbol = '.')

string tmp = Regex.Match(strToParse, @"([-]?[0-9]+)([s])?([0-9]+)?[." + decimalSymbol + "]?([0-9 ]+)?([0-9]+)?").Value;

if (tmp.Length > 0 && strToParse.Contains(tmp))

var currDecSeparator = System.Windows.Forms.Application.CurrentCulture.NumberFormat.NumberDecimalSeparator;

tmp = tmp.Replace(".", currDecSeparator).Replace(decimalSymbol.ToString(), currDecSeparator);
return double.Parse(tmp);


return double.NegativeInfinity;



1st, it works whatever the culture of computer(500.5 or 500,5)



2nd if the user types something that is not number, you just return NegativeInfinite, so you just need to check if returned value is not equal to NegativeInfinity to know if value is a correct one, or not.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305536%2fcustom-number-validation-not-working-properly-when-value-isnt-a-number%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    don't know if it helps, but I have that function I now use in all of my projects :



    public static double DoubleParseAdvanced(this string strToParse, char decimalSymbol = '.')

    string tmp = Regex.Match(strToParse, @"([-]?[0-9]+)([s])?([0-9]+)?[." + decimalSymbol + "]?([0-9 ]+)?([0-9]+)?").Value;

    if (tmp.Length > 0 && strToParse.Contains(tmp))

    var currDecSeparator = System.Windows.Forms.Application.CurrentCulture.NumberFormat.NumberDecimalSeparator;

    tmp = tmp.Replace(".", currDecSeparator).Replace(decimalSymbol.ToString(), currDecSeparator);
    return double.Parse(tmp);


    return double.NegativeInfinity;



    1st, it works whatever the culture of computer(500.5 or 500,5)



    2nd if the user types something that is not number, you just return NegativeInfinite, so you just need to check if returned value is not equal to NegativeInfinity to know if value is a correct one, or not.






    share|improve this answer



























      0














      don't know if it helps, but I have that function I now use in all of my projects :



      public static double DoubleParseAdvanced(this string strToParse, char decimalSymbol = '.')

      string tmp = Regex.Match(strToParse, @"([-]?[0-9]+)([s])?([0-9]+)?[." + decimalSymbol + "]?([0-9 ]+)?([0-9]+)?").Value;

      if (tmp.Length > 0 && strToParse.Contains(tmp))

      var currDecSeparator = System.Windows.Forms.Application.CurrentCulture.NumberFormat.NumberDecimalSeparator;

      tmp = tmp.Replace(".", currDecSeparator).Replace(decimalSymbol.ToString(), currDecSeparator);
      return double.Parse(tmp);


      return double.NegativeInfinity;



      1st, it works whatever the culture of computer(500.5 or 500,5)



      2nd if the user types something that is not number, you just return NegativeInfinite, so you just need to check if returned value is not equal to NegativeInfinity to know if value is a correct one, or not.






      share|improve this answer

























        0












        0








        0







        don't know if it helps, but I have that function I now use in all of my projects :



        public static double DoubleParseAdvanced(this string strToParse, char decimalSymbol = '.')

        string tmp = Regex.Match(strToParse, @"([-]?[0-9]+)([s])?([0-9]+)?[." + decimalSymbol + "]?([0-9 ]+)?([0-9]+)?").Value;

        if (tmp.Length > 0 && strToParse.Contains(tmp))

        var currDecSeparator = System.Windows.Forms.Application.CurrentCulture.NumberFormat.NumberDecimalSeparator;

        tmp = tmp.Replace(".", currDecSeparator).Replace(decimalSymbol.ToString(), currDecSeparator);
        return double.Parse(tmp);


        return double.NegativeInfinity;



        1st, it works whatever the culture of computer(500.5 or 500,5)



        2nd if the user types something that is not number, you just return NegativeInfinite, so you just need to check if returned value is not equal to NegativeInfinity to know if value is a correct one, or not.






        share|improve this answer













        don't know if it helps, but I have that function I now use in all of my projects :



        public static double DoubleParseAdvanced(this string strToParse, char decimalSymbol = '.')

        string tmp = Regex.Match(strToParse, @"([-]?[0-9]+)([s])?([0-9]+)?[." + decimalSymbol + "]?([0-9 ]+)?([0-9]+)?").Value;

        if (tmp.Length > 0 && strToParse.Contains(tmp))

        var currDecSeparator = System.Windows.Forms.Application.CurrentCulture.NumberFormat.NumberDecimalSeparator;

        tmp = tmp.Replace(".", currDecSeparator).Replace(decimalSymbol.ToString(), currDecSeparator);
        return double.Parse(tmp);


        return double.NegativeInfinity;



        1st, it works whatever the culture of computer(500.5 or 500,5)



        2nd if the user types something that is not number, you just return NegativeInfinite, so you just need to check if returned value is not equal to NegativeInfinity to know if value is a correct one, or not.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 20:13









        Siegfried.VSiegfried.V

        223215




        223215





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305536%2fcustom-number-validation-not-working-properly-when-value-isnt-a-number%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20