3 Replies - 524 Views - Last Post: 15 April 2013 - 01:32 PM

#1 Ghandi_Of_Codez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 26-March 13

Validating form with multiple Regular Expressions

Posted 15 April 2013 - 01:12 PM

Hey guys, So I made this code and can't tell exactly where it going wrong, everything looks right as far as I know. Here is the code tho, it is just suppose to match email, phone and address to each validation. Im sure it's a simple mistake I'm just not seeing. Any guidance will be much appreciated.

<!DOCTYPE html>

<HTML>

<head>

<script type='text/javascript'>

	function formValidate()
	{
	var email = document.getElementById("user_email").value;
	var phone = document.getElementById("user_phone").value;
	var street = document.getElementById("user_street").value;

	var alertmessage = "";
	var email_reg = /\w+\@\w+\.\w{1,3}/;
	var phone_reg = ^(\(?[0-9]{3}\)?)?[ .-]?[0-9]{3}[.-]?[0-9]{4}$^;
	var street_reg = ^[0-9]{1,7}[a-zA-z0-9]{2,35}\a*^;

		if(!email_reg.test(email)) {
			alertmessage += "Email is not valid \n";
		}
		if(!phone_reg.test(phone)) {
			alertmessage += "Phone number is not valid \n";
		}
		if(!street_reg.test(street)) {
			alertmessage += "Street address is not valid \n";
		}
		if(alertmessage != ""){
			alert(alertmessage);
				return false;
		}	
}

</script>

</head>

<body  style = "width:600px;margin-left:auto;margin-right:auto;">

<form action="handler.php" method="post"  onsubmit="return formValidate();">

<table border="1" align="center" width="100%">
	<tr>
		<th align="center" colspan="2">My Form</th>
	</tr>
	
	<tr>
		<td align="right" width="50%">User Email:</td>
		<td width="50%"><input type="text" name="user_email" id="user_email" value=""></td>
	</tr>
	
	<tr>
		<td align="right">User Phone Number:</td>
		<td><input type="text" name="user_phone" id="user_phone" value=""></td>
	</tr>
	
	<tr>
		<td align="right">User Street Address:</td>
		<td><input type="text" name="user_street" id="user_street" value=""></td>
	</tr>
</table>
<table border="0" align="center" width="100%">
<td align="center"><input type="submit" name="submit" value="Submit"></td>
</table>
</form>

</body>

</HTML>


Is This A Good Question/Topic? 0
  • +

Replies To: Validating form with multiple Regular Expressions

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,442
  • Joined: 12-December 12

Re: Validating form with multiple Regular Expressions

Posted 15 April 2013 - 01:17 PM

Your phone and street regex lack delimiters '/'. You cannot use '^' for this purpose.

This post has been edited by andrewsw: 15 April 2013 - 01:19 PM

Was This Post Helpful? 1
  • +
  • -

#3 Ghandi_Of_Codez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 26-March 13

Re: Validating form with multiple Regular Expressions

Posted 15 April 2013 - 01:27 PM

Haha, thank you very much for response. I laughed a little bit when I saw what I did :)
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,442
  • Joined: 12-December 12

Re: Validating form with multiple Regular Expressions

Posted 15 April 2013 - 01:32 PM

View PostGhandi_Of_Codez, on 15 April 2013 - 08:27 PM, said:

Haha, thank you very much for response. I laughed a little bit when I saw what I did :)

No worries. To clarify, in Perl and other regex-dialects, it is possible to use any non-whitespace character to delimit the regex, but not in JS.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1