Jump to content



Photo
- - - - -

Chapter 5 Pursue: Check For One @ In Email

pursue

  • Please log in to reply
4 replies to this topic

#1 Roy

Roy

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 11 June 2012 - 2:56 PM

I've been racking my brain over how to achieve this, but I'm stumped.

Here's the code I use in the relevant contact.js:

function process() {
'use strict';
//set initial check for validation to true because there are no errors yet
var okay = true;
//reference to email and comments elements
var email = document.getElementById('email');
var comments = document.getElementById('comments');
//validate email
if (!email || !email.value || (email.value.length < 6) || (email.value.indexOf('@') == -1)) {
  okay = false;
  alert('Please enter a valid email address');
}//end if
//validate comments
if (!comments || !comments.value || (comments.value.indexOf('<') != -1)) {
  okay = false;
  alert('Please enter your comments, without any HTML!');
}//end if
return okay;
}//end process() function
//event listener
function init() {
'use strict';
//call process upon submission of form
document.getElementById('theForm').onsubmit = process;
}//end init function()
window.onload = init;

  • 0

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,702 posts
  • LocationState College, PA (USA)

Posted 11 June 2012 - 3:22 PM

Sure thing. What you would do to confirm that there's just one @ is to confirm that the indexOf('@') equals the lastIndexOf('@'), assuming that neither equals -1. If the first occurrence of @ in a string is indexed at the same spot as the last occurrence, then there's only one.
  • 0

#3 Roy

Roy

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 12 June 2012 - 2:00 PM

Thank you!
  • 0

#4 markifornia

markifornia

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts
  • LocationSan Diego, California

Posted 8 August 2012 - 12:07 PM

Roy did you get this to work? I may have over complicated the logic or have taken an unnecessary route in my script in what is being pursued here.

See my modification to the script below:


// Get a reference to form elements.
var email = document.getElementById('email');
var comments = document.getElementById('comments');

// Validate the email address:
if (!email || !email.value || (email.value.length > 6) || (email.value.indexOf('@') == -1) || (email.value.lastIndexOf('@') == -1)) {

// Checks to see that the first instance or last instance is -1
if ((email.value.indexOf('@') != -1) || (email.value.indexOf('@') != -1)) {

// Then check if they are equal to each other.
if (email.value.indexOf('@') == email.value.lastIndexOf('@')) {
okay = false;
alert('Two of instances of @ found, please enter a valide email address!');
}

}


okay = false;
alert('Please enter a valid email address!);

}
  • 0

#5 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,702 posts
  • LocationState College, PA (USA)

Posted 10 August 2012 - 9:29 AM

You don't need this code:
if ((email.value.indexOf('@') != -1) || (email.value.indexOf('@') != -1)) {
Because it's in the previous conditional.
  • 0