I just have to validate the email address entered. To keep this simple, assume that a valid email address is one that contains an @ sign and a dot(period) with the dot after the @ sign. If the address is invalid, the application should display message like this, depending on what the problem is:
A valid email address must include an @ sign.
A valid email address must include a period after the @ sign.
Here is my JavaScript:
Code:
var $ = function (id) {
return document.getElementById(id);
}
var email_click = function () {
var emailAddress = $("email").value;
alert ( emailAddress );
}
var date_click = function () {
var dateEntry = $("date").value;
alert ( dateEntry );
}
window.onload = function () {
$("processEmail").onclick = email_click;
$("processDate").onclick = date_click;
}
You can use indexOf to find the location of a character in a string. It will return -1 if that character does not exist in the string.
Code:
var email_click = function () {
var emailAddress = $("email").value;
// Find the location of the @ symbol
var atPos = emailAddress.indexOf('@');
// Find the location of the dot, if there is one after the @ symbol
var dotPos = emailAddress.indexOf('.', atPos);
if(atPos == -1 || dotPos == -1) {
alert('Invalid email address');
} else {
alert('Valid email address');
}
}
Ok. I plugged that code in, after the var emailAddress line. I then entered my personal valid email address and it brings up the alert for an invalid address still. ??
var email_click = function () {
var emailAddress = $("email").value;
alert ( emailAddress );
}
..with that expression and then just enter my alert statements? Sorry, very new to this. I should also mention that this is just for testing through Aptana. Not sure if that matters or not.
var email_click = function () {
var emailAddress = $("email").value;
alert ( emailAddress );
}
:
to:
Code:
var email_click = function () {
var emailAddress = $("email").value;
if(emailAddress.match(/@+[0-9a-zA-Z_-]+\.[a-zA-Z_]/)){
alert('Email Address is valid!');
}else{
alert('Email Address is NOT valid!');
}
}
Last edited by NoEffinWay; 04-09-2012 at 12:15 PM.
Reason: Error in the RegEx
var email_click = function () {
var emailAddress = $("email").value;
if(emailAddress.match(/@+[a-zA-Z_]+\.[a-zA-Z_]/)){
alert('Email Address is valid!');
}
else{
alert('Email Address is NOT valid!');
}
}
but it still just gives me the 'Email Address in NOT valid' alert when I enter a valid email. ?
var $ = function (id) {
return document.getElementById(id);
}
var email_click = function () {
var emailAddress = $("email").value;
if(emailAddress.match(/@+[a-zA-Z_]+\.[a-zA-Z_]/)){
alert('Email Address is valid!');
}
else{
alert('Email Address is NOT valid!');
}
}
var date_click = function () {
var dateEntry = $("date").value;
alert ( dateEntry );
}
window.onload = function () {
$("processEmail").onclick = email_click;
$("processDate").onclick = date_click;
}
Bookmarks