0

Just started played around with jquery but I cant get a simple validation to work hehe.

Validator.js -

    $(document).ready(function(){

    var form = $("#contactForm");
    var name = $("#cfvname");
    var email = $("#cfvemail");
    var msg = $("#cfvmessage");

    name.blur(validateName);
    email.blur(validateEmail);

    form.submit(function(){
        if(validateName() & validateEmail() & validateMsg()){
            return true;
        }
        else{
            return false;
        }
    });

    function validateEmail(){
        var a = $("email").val();
        var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9­]+.[a-z]{2,4}$/;

        if(filter.test(a){
            email.removeClass("error");
            return true;

        } 
        else{
            email.addClass("error");
            return false;
        }
    }

    function validateName(){
        if(name.val().length < 5){
            name.addClass("error");
            return false;       
    } 
        else{
            name.removeClass("error");
            return true;
    }

    function validateMsg(){
        if(msg.val().length < 50){  
            msg.addClass("error");
            return false;
        }
        else{
            msg.removeClass("error");
            return true;
        }
    }
});

HTML -

<form id="contactForm" method="post" action="">
    <div>   
        <label for="dwc_name">Name</label>
        <br />
        <input type="text" id="cfvname" name="dwc_name" />
    </div>
    <div>       
        <label for="dwc_email">Email</label>
        <br />
        <input type="text" id="cfvemail" name="email" />
    </div>
    <div>
        <label for="dwc_message">Message</label>
        <br />
        <input type="textarea" id="cfvmessage" name="dwc_message" />
    </div>
    <div>
        <br />
        <input type="submit" id="send" value="Send" name="dwc_submit" />
    </div>
</form>

I just simply want to validate name and message for characters and email for a proper email, if it make sense. English is not my greatest language.

Im running all files from same directory. And they are included propertly since I made other things work whoa.

Anyway any help is appriciated.

And again, sorry for annoying English.

Dartsson
  • 1
  • 1
  • 1
    Please include relevant parts of the code directly in the question here on Stack Overflow, not only on an external service. – Anders Abel Apr 28 '12 at 07:06
  • If it's not absolutely necessary for your compatibility needs, I would recommend using native HTML5 + CSS3 validation. See [setCustomValidity](http://stackoverflow.com/questions/5272433/html5-form-required-attribute-set-custom-validation-message) – Femi Jun 01 '15 at 20:05

1 Answers1

0

this line is wrong

var a = $("email").val(); 

instead of it you need to use

var a = email.val();
jcubic
  • 61,973
  • 54
  • 229
  • 402