Hey guys I know there is a similar post on here but it didn't really give me any actual help on this topic:

It seems that I have the correct regular expressions in this however it doesn't want to follow my freaking instructions can you guys maybe show me where I'm going wrong and give me a short source code on how to rectify it. Code is below:

function validate(theForm)
	{
	var falseCounter = 0;
	var firstName = theForm.first_name.value;
	var lastName = theForm.last_name.value;
	var mail = theForm.email.value;
	var phoneNum = theForm.phone.value;
	var suburbs = theForm.suburb.value;
	var cities = theForm.city.value;
	var counties = theForm.country;
	
	var alphaTest = /^([A-Za-z+]\s)|([A-Za-z+]\s-\s[A-Za-z+]\s)$/;
	var emailTest = /(^[\w+]|[\w+.w+]@[\w+\.\w+]$)/;
	var numTest = /(^[00|0|+][\d+]$)/;
	
	
	
	
		if (alphaTest.test(firstName))
		{
			return true;
		}
		else
		{
			alert("Invalid name format.");
			return false;
			falseCounter ++;
		}
	
	
	
		if (alphaTest.test(lastName))
		{
			return true;
		}
		else
		{
			alert("Invalid name format.");
			return false;
			falseCounter ++;
		}
	
	

		if (emailTest.test(mail))
		{
			return true;
		}
		else
		{
			alert("Not a valid email address");
			return false;
			falseCounter ++;
		}
	
	

		if (numTest.test(phoneNum))
		{
			return true;}
		else
		{
			falseCounter ++;
			alert("Not a valid phone number.");
		}
	if (alphaTest.test(suburbs))
		{;
			return true;
		}
		else
		{
			falseCounter ++;
			alert("Invalid area format.");
		}

		if (alphaTest.test(cities))
		{;
			return true;
		}
		else
		{
			falseCounter ++;
			alert("Invalid area format.");
		}
	
		if (countries == "")
		{
			falseCounter++;
			alert("CHOOSE A FUCKING COUNTRY");
		}
		else
		{
		return true;
		}
	  
	
	if (falseCounter > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

This is in a separate .js file. basically i want it to return which areas are false in an alert (for now, at a later stage I will have to make the mark up change when this section is incorrectly completed) and then have a false counter to allow whether or not it can post because the way I had it was returning a single false/true test through alphaTest.test(firstName) which only checked if that was right and didn't care about the other ones

Recommended Answers

All 7 Replies

var alphaTest = /^([A-Za-z+]\s)|([A-Za-z+]\s-\s[A-Za-z+]\s)$/;
	var emailTest = /(^[\w+]|[\w+.w+]@[\w+\.\w+]$)/;
	var numTest = /(^[00|0|+][\d+]$)/;

1. the + signs are misplaced and you are specifying \s which will almost certainly not be present
2. I already posted a better emailRE in another thread
3. misused [brackets] and another misplaced + sign -- and are you trying to require international numbers?

1. the + signs are misplaced and you are specifying \s which will almost certainly not be present
2. I already posted a better emailRE in another thread
3. misused [brackets] and another misplaced + sign -- and are you trying to require international numbers?

hmmm yea I kinda figured that my expressions are a little bit off so i simplified the alpha to [A-Z][a-z+]

and yea i need to account for the fact that people will do that... basically i have a job_submit page that needs to be validated all I really want is the contact info to be correct as for the rest of the page it doesn't really need any input

and could you please send the url for that link

#
function validate_form()
#
{
#
var testVar=/^[a-zA-Z]+\s[a-zA-Z]+$/
#
if (!testVar.test(document.FORM.fullname.value))
#
{
#
alert("Invalid name format.");
#
return (false);
#
}
#
 
#
testVar=/^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
#
if (!testVar.test(document.FORM.email.value))
#
{
#
alert("The 'Email' is not valid.");
#
document.FORM.email.focus();
#
return (false);
#
}
#

this is what you sugested however wouldnt the + in this be considered as one or more and the . be conisderd as anying? would you not have to escape it with \

...
this is what you sugested however wouldnt the + in this be considered as one or more and the . be conisderd as anying? would you not have to escape it with \

testVar=/^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/ No, in the above pattern the . is just a dot indicating the possibility of a decimal point. Most symbols, when they appear between square brackets, are interpreted as members of a character class. There are a few exceptions: if ^ is the first symbol after [ then it negates the character class. Hyphen defines a range when it occurs between two letters or between two numbers. When it is the last item in a character class it is just a hyphen. Sometimes escaping a symbol in a character class makes no difference, so I guess you could say "when in doubt, escape." But the above pattern looks OK to me.

I guess you could say "when in doubt, escape."

Er, no. These two tests

alert(/[d]/.test("d"))
	alert(/[\d]/.test("d"))

return opposite results.

The motto should read, "when in doubt, consult the manual."

Er, no. These two tests

alert(/[d]/.test("d"))
	alert(/[\d]/.test("d"))

return opposite results.

The motto should read, "when in doubt, consult the manual."

LMAO!! Good but I think the previouse reply was good thanks, these just need a bit of going through once i understand them they should be fine.

I'll porbably post something one here again monday or so

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.