Naeem
Naeem

Reputation: 19

RequiredFieldValidator to allow single white space

I have a RequiredFieldValidator on a textbox that fires if no text is entered. But it should not fire if I enter single white space. Right now If i enter single white space it still fires RequiredFieldValidator , is there a way around it. I am using asp.net web forms

Upvotes: 0

Views: 672

Answers (1)

VDWWD
VDWWD

Reputation: 35564

You can use a CustomValidator. You then need to create your own javascript function to validate the input and assign that function to the validator as the ClientValidationFunction. You can make that validator as complex as you want, as long as you return a boolean with args.IsValid

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator"
 ClientValidationFunction="checkForWhiteSpace" ControlToValidate="TextBox1" 
 ValidateEmptyText="true"></asp:CustomValidator>

<script type="text/javascript">
    function checkForWhiteSpace(oSrc, args) {
        if (args.Value === "") {
            args.IsValid = false;
        } else {
            args.IsValid = true;
        }
    }
</script>

Upvotes: 1

Related Questions