Home > Mobile >  Validating C#'s Long Variable value in Javascript
Validating C#'s Long Variable value in Javascript

Time:06-16

I have a form wherein a user can enter a value associated with a nullable long property of my ViewModel. But because I have an 'onblur' event on that text box, I am trying to validate the entered value in my textbox.onblur() event and ensure that it does not exceed the C#'s, long.MaxValue. Here is my "blur" code on that text box.

var value = $(this).val();
console.log(value > 9223372036854775807);

if (value<= 1 || value > 9223372036854775807) {             
            $('#divValueError').text("Invalid Value!");
            return false;
        }

But Javascript is returning false on that console.log statement if I enter 9223372036854775808. How do I check if the number entered by the user falls within the limits of a C# long value?

I understand 64 bit numbers are not supported by Javascript. I also could not get my [Range] data annotation on that property to fire before this blur event is called, even though I tried

if (!$(this).valid()) {

            return false;
        }

Please let me know how I can throw a client side error if the value entered by the user falls outside the boundaries of a C#'s long data type value.

Thanks!

CodePudding user response:

Perfect Answer : I am Providing example that will solve your problem. In this , you first have to convert your value in string and then in biginteger. Reference link added for better guidance.

var valuecompare = BigInt("9223372036854775808");
var valuebase = BigInt("9223372036854775807");

console.log(valuecompare);
console.log(valuebase);

if (valuecompare > valuebase) {             
            console.log('greater value');
        }
else{
    console.log('less value');
}

Reference Link : https://www.smashingmagazine.com/2019/07/essential-guide-javascript-newest-data-type-bigint/

  • Related