Jump to content

Help writing Javascript form validation script


drake3713

Recommended Posts

Ok I've been working on this script for a few days now, and I'm starting to become really frustrated. Despite this being a relatively simple script, and having a decent amount of experience using Javascript, I just can't get the script to behave how I want. This script is going on a website where users can subscribe to our newsletter by filling out a short form that has a text box for their email address, A CAPTCHA script to prevent abuse, and then a submit button. I'm trying to do some simple form validation to make sure they enter an email address in the correct format (someone@website.com) before the user input gets passed on to my PHP/MySQL. Like I mentioned before, I do have some experience working with Javascript, but I'm definitely not an expert, so hopefully someone who has a better understanding can help me spot any syntax errors (or whatever the problem is). This script is based off some other email validation scripts I've seen before, but they were either too simple (and let too many bad email addresses pass through), or very complex (and difficult to integrate with my existing code). So I decided to find a balance between the two and write my own. I've been working on this for about 2 days and have tried virtually every possible combination of syntax, so I'm posting this out of desperation. Hopefully I'm just overlooking a simple mistake. Anyway, here's the code (with comments to explain):

function checkEmail(email) { //the variable email is taken directly from the textbox valuevar atPos = email.indexOf("@"); //get the position of the at signvar dotPos = email.lastIndexOf("."); //get the position of the last period//I'm assuming my problem has to do with the conditions in the if statement belowif ((atPos <= 0) || (dotPos = -1) || ((dotPos - atPos) = 1) || (dotPos = (email.length - 1))) {	document.getElementById("form_validation_notification").style.display = "block"; //display warning	return false; //don't submit form}else {	document.getElementById("form_validation_notification").style.display = "none"; //hide warning	document.getElementById("submit_button").disabled = true; //disable submit button to prevent clicking twice	return true; //submit form}}

Hopefully that was easy to understand. Thanks in advance for any help.

Link to comment
Share on other sites

Just to clarify the conditions in that IF statement:(atPos <= 0) checks to see if the user forgot to put an @ symbol, or if the @ symbol is the first character, meaning they didn't enter the username portion of the email address at the beginning.(dotPos = -1) checks to see if they entered at least one period somewhere in the email address.((dotPos - atPos) = 1) checks to see if they entered the first part of the domain name between the @ sign and the last period (a.k.a the last period doesn't come right after the @ symbol; there should be some kind of text between them).(dotPos = (email.length - 1)) checks to see if the period isn't the last character in the string, meaning they forgot the last part of the domain name at the end.If any of these statements are true, it means there is a problem with the user's input and it should execute that first block of code (display warning and don't submit form).Otherwise, the email address is fine, and the block of code under ELSE will be executed (hide warning, disable submit button, and submit the form).I wish I could be more specific about the problem I'm having, but I've tried many different combinations and they all seem to make the script behave a little differently. But it never quite acts right.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...