Home › Forums › JavaScript › Where am i going wrong? Simple Function ?!
- This topic is empty.
-
AuthorPosts
-
May 19, 2012 at 7:32 pm #38145
JohnMotylJr
ParticipantDisclaimer: 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 #103115JohnMotylJr
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;
}May 19, 2012 at 9:35 pm #103116JohnMotylJr
ParticipantMaking 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 10:13 pm #103119JohnMotylJr
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+)*
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.