Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Where am i going wrong? Simple Function ?!

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #38145
    JohnMotylJr
    Participant

    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.

    #103115
    JohnMotylJr
    Participant
    /* 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;
    }
    #103116
    JohnMotylJr
    Participant

    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?

    #103119
    JohnMotylJr
    Participant

    @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 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.