简体   繁体   English

jQuery验证从头开始

[英]JQuery validation from scratch

First time I'm touching JQuery let alone javascript. 第一次接触JQuery,更不用说javascript了。 Worked with this for about 20 minutes and need a wee-bit-o help. 为此工作了大约20分钟,并且需要一点点帮助。 My javacript: !!!EDIT!!! 我的Javacript:!!!编辑! just changed to this now 现在刚改成这个

function val_fade1() {
    $("#firstname").bind('keydown', function(){
        var firstnameCount = $(this).val().length;
    }
    $("#lastname").bind('keydown', function(){
        var lastnameCount = $(this).val().length;
    }
    $("#email").bind('keydown', function(){
        var emailCount = $(this).val().length;
    }
    $("#password").bind('keydown', function(){
        var passwordCount = $(this).val().length;
    }
    if(firstnameCount < 2) {
        $("#req_first").show("slow");
    } else if(lastnameCount < 2) {
        $("#req_last").show("slow");
    } else if(emailCount < 5) {
        $("#req_email").show("slow");
    } else if(passwordCount < 5) {
        $("#req_pass").show("slow");
    } else {
        $("#register1").fadeOut();
    }
}

So now what I'd like to have happen is like say you were entering your firstname and it was less than 2 characters, the label that I have currently hidden with CSS that says (please enter a longer name) would show up. 因此,现在我想发生的事情就是说您输入的名字少于2个字符,那么我目前在CSS中隐藏的标签会显示(请输入一个较长的名称)。 As well as the other fields (lastname, email, password). 以及其他字段(姓氏,电子邮件,密码)。 But if all of them are filled out correctly, the "register1" div will fade out. 但是,如果所有参数均正确填写,“ register1” div将消失。

Here are the parts of my HTML just in case you would need it: 这是我的HTML的一部分,以防万一您需要它:

<label>First Name
    <label id="req_first">REQUIRED*<label> 
</label>
<input type="text" name="firstname" id="firstname" class="input-text2" />

<label>Last Name 
    <label id="req_last">REQUIRED*</label>
</label>
<input type="text" name="lastname" id="lastname" class="input-text2" />

<label>E-Mail Address 
    <label id="req_email">REQUIRED*</label>
</label>
<input type="email" name="email" id="email" class="input-text2" />

<label>Password 
    <label id="req_pass">REQUIRED*</label>
</label>
<input type="password" name="password" id="password" class="input-text2" />

And the CSS hides the REQUIRED* labels simply like this: CSS会像下面这样隐藏REQUIRED *标签:

<style>
#req_first {
    display:none;
}
#req_last {
    display:none;
}
#req_email {
    display:none;
}
#req_pass {
    display:none;
}
</style>

I'm very, very new to JS and JQuery so if you could please explain to me how to make this work , I'd be very greatful, I work mainly with PHP and have PHP validation, but I like the appeal of JQuery fade ins and the PHP could be backup if someone decided to turn off their JS. 我对JS和JQuery非常陌生,所以如果您能向我解释如何进行这项工作,我将非常感激,我主要使用PHP并进行PHP验证,但是我喜欢JQuery淡淡的吸引力如果有人决定关闭其JS,则可以备份ins和PHP。

Thanks so much guys! 非常感谢你们! -mike -麦克风

首先纠正您的HTML和CSS,我创建了一个原型,这可能会对您有所帮助。

DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM