1

I am practising client side validations with server side. I have written a code in client side to validate name and it is returing the value. But problem is that for incorrect values the error message is not displaying as soon as focus lost from the text box. My implementation is as:

Updated

 <script type="text/javascript">
    function validateText(osrc, args) {
        var textvalue = args;
        var pattern = /^[a-zA-Z.]{3,25}$/
        if (textvalue != null || textvalue != "") {
            if (pattern.test(textvalue)) {

                args.isValid = true;
            }
            else
                args.isValid = false;
        }
        else args.isValid = false;
        return;
    }
</script>

and aspx page contents are

<asp:TextBox runat="server" ID="txtFirstName" CssClass="form-control" />
            <asp:CustomValidator runat="server" Display="Dynamic" CssClass="text-danger"
                 ControlToValidate="txtFirstName" ToolTip="Incorrect Text" ErrorMessage="*"
                 EnableViewState="false" ValidateEmptyText="true" EnableClientScript="true"
                 Enabled="true" ClientValidationFunction="validateText"></asp:CustomValidator>//This validator is not showing the error message 

What's wrong with it, help required.

Hassaan
  • 3,931
  • 11
  • 34
  • 67

1 Answers1

2

You must set the value of args.IsValid to TRUE or FALSE instead of just returning true/false. Also you need to use args.Value (notice V is uppercase) to set textvalue correctly. And you are only supposed to RETURN IF/WHEN the expression is valid:

JavaScript:

function validateText(osrc, args) {
    var textvalue = args.Value; //notice V is uppercase!
    var pattern = /^[a-zA-Z.]{3,25}$/
    if (textvalue != null || textvalue != "") {
        if (pattern.test(textvalue)) {
            args.IsValid = true;
            return; //notice we ONLY return when the expression IS valid
        }
        args.IsValid = false;
    }
    args.IsValid = false;
}

ASPX:

<div class="col-md-10">
    <asp:Label ID="Label1" runat="server" AssociatedControlID="txtFirstName" 
               CssClass="col-md-2 control-label">First Name</asp:Label>
    <asp:TextBox runat="server" ID="txtFirstName" CssClass="form-control" />
    <asp:CustomValidator ID="CustomValidator1" runat="server" 
                         Display="Dynamic" ControlToValidate="txtFirstName" 
                         ToolTip="Incorrect Text" ErrorMessage="*" 
                         EnableViewState="false" 
                         ValidateEmptyText="true" 
                         EnableClientScript="true" 
                         Enabled="true" 
                         ClientValidationFunction="validateText">
        Please input a valid First Name!
    </asp:CustomValidator>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  
                                ControlToValidate="txtFirstName"
                                CssClass="text-danger" 
                                ErrorMessage="First name is required" 
                                ToolTip="First name is required." 
                                EnableViewState="False" />
</div>
lucidgold
  • 4,432
  • 5
  • 31
  • 51
  • i have done and debug the function is returning the value but the error is still not displaying? – Hassaan Jun 20 '14 at 20:12
  • Please update your code in your question so we know what you have so far – lucidgold Jun 20 '14 at 20:12
  • I have updated my code, there was an issue with the return statement. You are ONLY supposed to RETURN IF/WHEN the expression is VALID. Also I made some changes to the ASPX. You will now see "Please input a valid First Name!" error – lucidgold Jun 20 '14 at 20:36
  • 1
    The comments about only `return`ing when the field is valid are incorrect. The only thing that matters is that `args.IsValid` is correct when the function returns (naturally or via `return;`). See https://stackoverflow.com/questions/31095104 – Greg B Aug 22 '17 at 11:11