Hy guys i know there is one thread about validation ...
but i cant find what is messing up this simple form ...
cant get correct result .. iam new in javascript so if you have any idea or what is wrong would you let me know .. i understand this script but iam not sure if its correct.. THanks guys..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Emerald Bank</title>
<style type="text/css">
.table{
	border:3px black solid;
	width:300px;
	margin-left:100px;
	padding:20px;
	}
	.table tr{
		height:40px;
		}
	.table td{
		border-bottom:1px solid black;
		height:37px;
		}
</style>
</head>
<body>

<form name="verification"  onsubmit="return validate();" method="post">
<div id="container">
<h1>Registration</h1>
<div class="table">
<table>
<tr>
<td>*First Name:</td><td><input type="text" maxlength="50" id="firstname"  /></td>
</tr>
<tr>
<td>*Second Name:</td><td><input type="text" maxlength="50" id="surename" /></td>
</tr>
<tr>
<td>*Gender:</td><td><label for="male">Male</label><input type="radio" name="gender" id="male"/></input>&nbsp;&nbsp;&nbsp;&nbsp;<label for="female">Female</label><input type="radio" name="gender" id="female"/></input></td>
</tr>
<tr>
<td>*Email:</td><td><input type="text" maxlength="50" id="mail" /></td>
</tr>
<tr>
<td>*City:</td><td><input type="text" maxlength="50" id="city" /></td>
</tr>
<tr>
<td>*Street:</td><td><input type="text" maxlength="50" id="street" /></td>
</tr>
<tr>
<td>*Post code:</td><td><input type="text" maxlength="50" id="post" /></td>
</tr>
<tr>
<td><input type="submit"  value="Verify"  /></input>&nbsp;&nbsp;&nbsp;<input type="reset" id="reset"  /></input></td>
</tr>
</table>
</form>
</div>
</div>

</body>

<script type="text/javascript">
document.getElementById('firstname').focus()

function validate(){
	var x = document.forms["verification"] ["firstname"].value;
	var y = document.forms["verification"] ["surename"].value;
	var a = document.forms["verification"] ["mail"].value;
	var b = document.forms["verification"] ["city"].value;
	var c = document.forms["verification"] ["street"].value;
	var d = document.forms["verification"] ["post"].value;
	var name = document.getElementById('firstname').value;
	var sname = document.getElementById('surename').value;
	var atpos = a.indexOf("@");
	var dotpos = a.indexOf(".");
	
	
	/*    conditions*/
	if (x==null ||x==" ")
	{
	alert("First name must by filled out");
	return false;
	}
	else if (y==null ||y==" ")
	{
	alert("Surename must by filled out");
	return false;
	}
	
	else if(atpos<1 || dotpos<atpos+2 || dotpos>=a.length)	{
	alert("Not a valid email address");
	return false;
	}
	
	
	else if (b==null ||b==" ")
	{
	alert("City field must by filled out");
	return false;
	}
	
	
	
	else if (c==null || c==" ")
	{
	alert("Street field must by filled out");
	return false;
	}
	
	
	else if (d==null || d==" ")
	{
	alert("Post code must by filled out");
	return false;
	}

else{
	alert("Thank you for registration" +firstname+ +surename);
	
	}

}
	

</script>
</html>

Recommended Answers

All 10 Replies

Traditionally, I've always used the document.getElementById('Element ID') method of retrieving a value from a text box. Then I would test to see what was put in that box, and if I didn't like it, throw up an alert and clear the box out, then set focus to said box.

You look like your doing the same thing, but I've never really seen that method of getting the value from a text box, addressing it like it's a multi-dimensional array.

I'm not 100% sure what your issue is, your logic appears sound, but my first guess would be in the way your accessing the values, simply because I don't recognize that method.

Thank you for this .. for quick reply ..
ill try it your way it looks actually more reasonable :); As i said iam new in javascript and i just started to explore wast amount of scripts and all the possibilities ...but sometimes the best way to learn is the ask method. :)
So thanks again , if someone has some other way to do this more easy feel free to post it ...

I agree, the best way to learn is to ask. I also think that the best way to teach is to not do it for them, but help them do it themselves.

If you are willing to use FireFox, the browser has a great tool for JavaScript called 'Firebug'. It is very easy to use and allows you to see where the error is right away. It should help you learn JavaScript too.

try using document.getElementById('Element ID').value instead of document.forms["verification"] ["firstname"].value...

Hello again .. i successfully find the way to validate form. Now i found new problem ..The form should open in new window with which it does.Then there is the continue button .After that the form should be validated and processed .
Basically what idlike to know is how do i nest this verify function ...

oh and one more what is the best way to verify email .. this what i did .. is working but not as well as i need it to .. doesnt take format like abc-abc@abc.com

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<title>Emerald Bank</title>
		
		
		<script type="text/javascript">
		


	




			
 function show()
		{
		var mywindow = window.open('','', 'width=500, heigh=500')
		mywindow.document.write("<h1>Please Check Your details</h1>")
		mywindow.document.write("<table style='border:1px black solid'>")
		mywindow.document.write("<tr>")
		mywindow.document.write("<td><p>Name:"+document.form.name.value+ "</p></td>");	
		mywindow.document.write("</tr>")
		mywindow.document.write("<tr>")
	 	mywindow.document.write("<td><p>Address:"+document.form.address.value+"</td>");	
		mywindow.document.write("</tr>");
		mywindow.document.write("<tr>");
		mywindow.document.write("<td><p>Number:"+document.form.number.value+"</p></td>");
		mywindow.document.write("</tr>");
		mywindow.document.write("<tr>");
		mywindow.document.write("<td><p>Email:"+document.form.mail.value+"</p></td>");
		mywindow.document.write("</tr>");
		mywindow.document.write("<tr>");
		mywindow.document.write("<td><p>Type:"+document.form.type.value+"</p></td>");
		mywindow.document.write("</tr>");
		mywindow.document.write("<tr>");
		mywindow.document.write("<td><p>Day:"+document.form.day.value+"</p></td>");
		mywindow.document.write("</tr>");
		mywindow.document.write("<tr>");
		mywindow.document.write("<td><p>Month:"+document.form.month.value+"</p></td>");
		mywindow.document.write("</tr>");
		mywindow.document.write("<tr>");
		mywindow.document.write("<td><p>Year:"+document.form.year.value+"</p></td>");
		mywindow.document.write("</tr>");
		mywindow.document.write("<tr>");
		mywindow.document.write("<td><input type='button' value='continue' onclick='verify()' ");
		mywindow.document.write("</tr>");
		mywindow.document.write("</table>");
	
			
				}	
			
				
		function  verify(){
		
			var x=document.getElementById('name').value;
			var exp =/[a-z | A-Z]/;
			if(!exp.test(x)){
				alert("error")
				return false;
			}
				else{
					alert("ok")
					}	
								
			var x=document.getElementById('address').value;
			var exp =/[a-z | A-Z]/;
			if(!exp.test(x)){
				alert("error")
				return false;
			}
				else{
					alert("ok")
					}		
			
			var x=document.getElementById('number').value;
			var exp =/[0-9]+/;
			if(!exp.test(x)){
				alert("error")
				return false;
			}
				else{
					alert("ok")
					}
					
			var x=document.getElementById('mail').value;
			var exp =/^[a-z | A-Z | 0-9]+([_][a-z | A-Z | 0-9]+)*@[a-z | A-Z | 0-9]+([-][a-z|0-9]+)*(([.][a-z]{2,3})|([.][a-z]{2}){2})$/;
			if(exp.test(x)){
				alert("ok")
				return true;
			}
				else{
					alert("false")
					}
				}
				
				
			
			</script>
		
		
		
		
		
		
	</head>
	<body>
		
		
		<form name="form">
		<table style="border:1px black solid">
			<tr>
				<td>Enter Name:</td><td><input type="text" size="20" maxlength="50" id="name"/></td>
			</tr>
			<tr>
				<td>Enter Address:</td><td><textarea cols="15" rows="4" id="address"></textarea></td>
			</tr>
			<tr>
				<td>Enter Telephone Number:</td><td><input type="text" size="20" maxlength="50" id="number"/></td>
			</tr>
			<tr>
				<td>Enter Email:</td><td><input type="text" size="20" maxlength="50" id="mail"/></td>
			</tr>
			<tr>
				<td>Type of account</td><td><select id="type">
				<option>Basic</option>
				<option>Silver</option>
				<option>Gold</option>
				<option>Platinum</option>
				</select>
				</td>
			</tr>
			<tr>
				<td>Date of opening</td><td>
				<select id="day">
					<option>1</option>
					<option>2</option>
					<option>3</option>
					<option>4</option>
					<option>5</option>
					<option>6</option>
					<option>7</option>
					<option>8</option>					
					<option>9</option>
					<option>10</option>
					<option>11</option>
					<option>12</option>
					<option>13</option>
					<option>14</option>
					<option>15</option>
					<option>16</option>
					<option>17</option>
					<option>18</option>
					<option>19</option>
					<option>20</option>
					<option>21</option>
					<option>22</option>
					<option>23</option>
					<option>24</option>
					<option>25</option>
					<option>26</option>
					<option>27</option>
					<option>28</option>
					<option>29</option>
					<option>30</option>
					<option>31</option>
					</select>
					<select id="month">
					<option>January</option>
					<option>February</option>
					<option>March</option>
					<option>April</option>
					<option>May</option>
					<option>Jun</option>
					<option>July</option>
					<option>August</option>
					<option>September</option>
					<option>Oktober</option>
					<option>November</option>
					<option>December</option>
					</select>
					<select id="year">
						<option >2011</option>
					</select>
					</td>
			</tr>
			<tr>
				<td><input type="button" value="Submit" onclick="show()" /></td>
			</tr>
		</table>
		</form>
		
		
		
	</body>
</html>

What do you mean by nested your verification? If you need to verify each field, you should have a verification to test each of the field you are testing unless those fields have the same testing format approach.

Speaking of regex for email, I have one but forgot it right now. It is not in this computer, so I can't give you anything until I have an access to the computer. Sorry.

I ment that i need to call the function verify but that is nested inside the function show .. (button continue= on click) I know there is some way to do it or command . I know the logic of how it should be but iam probably missing some step .And i looking for that one step :) now so thanx for your reply.:)

Oh, so you want the verify() to be working in your new pop up window? The reason it does not working because the pop up window doesn't contain the script in it but whatever HTML you write out. One way to do is to add the whole function definition including script tag to the new pop up window content. Or instead of popping up a new window, you could rerender the page with the verification form. That way you don't need to add the function content to the new pop up window again.

By the way, the regex I use for verifying e-mail is

/*
/[^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}/

It means that the starting of e-mail can be anything (including a dot) but a '@' or white spaces. Then, there will be only @ in the whole email in the middle of something. Then after the @ it must contain at least a letter or number before a dot, but there could be multiple part of character & dot. Last, it must contain at least 2 character behind the last dot.
*/

If you do not consider an email starting with a dot as an email, this regex will fail to do that. However, you could simply check for an email starting with a dot anyway.

Thank you Taywin this is what i did

<td><input type="submit" value="Submit" onclick="return verify()" /></td>

changed the button type to submit , then i added into a form onsubmit=show();

it seems to working so far ,i cannot be sure this syntax is correct until i have some database created
and see what it actually does especially the return commands .
i dont really know how to use them ... :)
Anyway thank you for help and for the email regexp ill certainly try it look much clearer ...:)

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.