Grow your CSS skills. Land your dream job.

Where am i going wrong? Simple Function ?!

  • # May 19, 2012 at 7:32 pm

    Disclaimer: I know Javascript, but i dont know JQuery.

    All i am doing is testing if the input’s value is an email. If true than allow, if false than alert.

    /* I normally wouldnt use an array but im doing stuff like this to practice and teach myself the syntax */
    var _inputArray = $([ "#name", "#email", "#subject", "#message" ]);
    var _regexEmail = /^([0-9a-zA-Z]([.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][w]*[0-9a-zA-Z].)+[a-zA-Z]{2,9})$/;
    var _validate = function ($e)
    {
    $e.preventDefault();
    alert(_inputArray[1]);

    if ($("_inputArray[1]:text").val().match( _regexEmail )) //condition of == true?
    {
    alert("Submit");
    return;
    }
    else
    {
    alert("fail");
    }

    alert("It must be the if statement condition logic!?");
    };


    $(document).ready(function()
    {
    alert(_inputArray[1]);
    $('#button').click(_validate);

    });

    The html::






    It’s very frustrating for me learning new syntax because this took me a few hours to put together :( I would really value any input from yall.

    # May 19, 2012 at 8:12 pm
    /* This if statement is returning the false alert only but slightly fixed, still need alot of help with this */
    if ($( _inputArray[1] ).val().match( _regexEmail ))
    {
    alert("Your amount contains valid characters");
    return;
    }
    # May 19, 2012 at 9:35 pm

    Making more progress, i think i figured it out. Is this good practice?

    /* Updated, still needs work. Im still learning */
    var _inputArray = $([ "#name", "#email", "#subject", "#message" ]);
    var _regexEmail = /^([0-9a-zA-Z]([.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][w]*[0-9a-zA-Z].)+[a-zA-Z]{2,9})$/;
    var _validate = function ($e)
    {
    $e.preventDefault();

    if ($( _inputArray[1] ).val().match( _regexEmail )) return $(document.body).append("It is True");
    else return $(document.body).append("It is False");

    };


    $(document).ready(function()
    {
    $('#button').click(_validate);
    });

    Any suggestions?

    # May 19, 2012 at 9:53 pm

    Not to stray too far from your actual question, but this

    ^([0-9a-zA-Z]([.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][w]*[0-9a-zA-Z].)+[a-zA-Z]{2,9})$

    will reject valid email addresses, including several of mine. Specifically, characters like * and + are valid in an email address. Also, this regex will accept things like email@.com as a valid address, when it’s not. And that’s not considering things like local addresses (admittedly, probably not something you need to worry about) that don’t have a TLD.

    The best advice I’ve found on this issue is as follows:

    Don’t go overboard in trying to eliminate invalid email addresses … you don’t really know whether an address is valid until you try to send an email to it.
    http://www.regular-expressions.info/email.html

    This regex is touted as fully-RFC822 compliant, but is largely impractical for obvious reasons.

    I usually hold off on email validation (just checking that the field is not empty, and/or allowing HTML5-capable browsers to check it) until server-side, where a better check can be made (e.g., using PHP’s filter_var()).

    # May 19, 2012 at 10:13 pm

    @traq, Thanks for catching that. Normally whenever i use regex i go into my Visual Studio and grab one (ive become lazy). And yeah, i hear what your saying in regards to not trying to go over the top. I know that if people want me to contact them they will enter their appropriate email, nor am i trying to prevent from spammers (because the form im making will only be given to a few people, nothing crazy).

    Thanks for the resources and the heads up.

    Going with this simple regex

    w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".