Question

i created CustomValidator with js function in wizard view just to check password length. And he dosn`t works, sorry for my newbieness.

js function:

 <script type="text/javascript">
    function clientValidate(sender, args) {
        if (args.Value.length < 5 ||args.Value.length > 30) {
            args.IsValid = false;
        }
    }
</script>

asp fields:

<asp:TextBox ID="tbPassword1" runat="server" TextMode="Password"></asp:TextBox>
<asp:CustomValidator ID="CustomValidatorPassLength" runat="server" display="Dynamic"     ControlToValidate="tbPassword1" ClientValidationFunction="clientValidate" ErrorMessage="Slaptažodis turi būti nuo 5 iki 30 simbolių ilgio." ForeColor="Red"></asp:CustomValidator>

Thanks and sorry!

Was it helpful?

Solution

The ASP Custom Validator will fire only when the user attempts to submit the webform via a button click, etc. If you want to run the validation during a client-side event, you'll have to use Javascript, not an ASP server control.

See this JS Fiddle Demo for an example of what you could do. The code is below.

HTML

<input type="text" class="password" id="mytextbox"></input><span class="errormsg">Password length invalid</span>

JQuery / Javascript

$(function(){
    // This is to add the "On Change" event to your textbox.
    $('#mytextbox').change(function(){
        if(!validateFunction($(this))){
            $(this).next('.errormsg').fadeIn({duration: 400});
        } else {
            $(this).next('.errormsg').fadeOut({duration: 100});
        }
    });
})

// This is the function that validates your checkbox. It returns true or false.
function validateFunction(obj){
    if(obj.hasClass('password')){
        var txt = obj.val();
        return (txt.length > 5 && txt.length <= 30);
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top