CustomValidator for Decimal Number using JavaScript in ASP.Net

nauna
 
on Jun 13, 2019 02:21 AM
Sample_187911.zip
2944 Views

i am using this regular expression to validate decimal point it works fine

 

but i want to add one more logic in it

if user type 0.30 it says invalid i want it to be valid but if user type 00.3 this should be invalid i guess in below expression if make it this so this will be good 0.30 but i want to add one more logic that if usre type 00.30 means in start 2 zero is invalid how to do that

^[0-9]\d*(\.\d+)?$
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtofferrate" CssClass="validation" Display="Dynamic" ErrorMessage="Valid Decimal Point Required" ValidationExpression="^[1-9]\d*(\.\d+)?$" ></asp:RegularExpressionValidator>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 13, 2019 03:12 AM

Hi nauna,

With RegularExpressionValidator it is not possible. For this you need to use CustomValidator and write your own logic.

Check this example. Now please take its reference and correct your code.

HTML

<asp:TextBox runat="server" ID="txtofferrate" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="Validate"
    CssClass="validation" Display="Dynamic" ControlToValidate="txtofferrate" ErrorMessage="" ForeColor="Red" />

JavaScript

<script type="text/javascript">
    function Validate(sender, args) {
        var offerRate = document.getElementById(sender.controltovalidate).value;
        var regex = /^[0-9]\d*(\.\d+)?$/;
        if (regex.test(offerRate)) {
            if (offerRate.split('.').length > 1) {
                var value = offerRate.split('.')[0];
                if (parseInt(value) == 0) {
                    if (value.length > 1) {
                        sender.innerHTML = "Valid Decimal Point Required."
                        args.IsValid = false;
                    }
                }
            }
            else {
                args.IsValid = true;
            }
        } else {
            sender.innerHTML = "Valid Decimal Point Required."
            args.IsValid = false;
        }
    }
</script>

Screenshot