Can anyone see what is wrong with my code here, I am simply trying to validate form input in the following ways:

""JavaScript Data Validation of Visitor's name field Only
must be validated as follows:

a) the length of the input is bigger than 0 and less/equal to 14;
b) only letters a to z (lower case), "-" (dash) and " " (space) are allowed,
c) the "-" (dash) and " " (space) letters must be entered,
d) the "-" or the " " letter must not be either the first or the last letter entered,
e) "-" must not be the immediate neighbour or adjacent (before or after) to a " ",
f) "-" or " " must not be the immediate neighbour (adjacent) of itself."

The script part:

<script language="javascript" type="text/javascript">

			function checkinput(n)
				{
					var names = document.interestform.name.value;
					var len = names.length;
					var symbols = /\!\"\£\$\%\^\&\*\(\)\_\+\{\}\:\@\~\?\>\<\|\¬\`\=\[\]\;\#\,\.\/\¦/;
					var nameformat = /^.+ .+-.+$/;
					var telno = document.interestform.telno.value;
					var telnoformat = /^.+{4,6} .+{7}$/;

				  if ((names.length == 0) || (names.length > 14))
						{
							alert ("Name must be no more than 14 characters long.");
							return false;
						}
					else
						{
							return n;
						}
					if ((names.toLowerCase() !== names) && (names.match(symbols)))
						{
							alert ("Name must be entered in lower case with no");
							return false;
						}
					else
						{
							return n;
						}
					if (!(names.test(nameformat)))
						{
							alert ("The format for Name must be first name followed by second name with a '-' in the middle of the second name.");
							return false;
						}
					else
						{
							return n;
						}
					if (!(telno.test(telnoformat)))
						{
							alert ("Telephone number must be area 4-digit area code in brackets followed by a space and then 7 more digits. e.g. (1234) 1122334");
							return false;
						}
					else
						{
							return n;
						}
				}

    </script>

The form part:

<div id="main">
			<h3>Interest Form</h3> <!-- make header -->
			<form name="interestform" action="thankyou.html" method="post">
				<table border=0 width="300px" cellpadding=4>
					<tr>
						<td>Name:<sup class="condition">*</sup><br><sub>E.g (Firstname Surname)</sub></td>
						<td><input type="text" name="name"></td>
					</tr>
					<tr>
						<td>Tel. Number:<sup class="condition">*</sup><br><sub>E.g (0123) 1122334</sub></td>
						<td><input type="text" name="telno"></td>
					</tr>
					<tr>
						<td>Location:<sup class="condition">*</sup></td>
						<td>
							<select name="location" value="default">
								<option value="default">Select Location</option>
								<option value="syorks">South Yorkshire</option>
								<option value="england">Rest of England</option>
								<option value="wales">Wales</option>
								<option value="scotland">Scotland</option>
								<option value="ireland">Ireland</option>
								<option value="eu">European Union</option>
								<option value="restofworld">Rest of the World</option>
								<option value="other">Other</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>Email:<br><sub>E.g. (abc@hotmail.com)</sub></td>
						<td><input type="text" name="email" value=""></td>
					</tr>
					<tr>
						<td><sup class="condition">* Denotes a required field.</sup></td>
						<td></td>
					</tr>
					<tr>
						<td><input type="submit" value="Submit" onClick="checkinput(n)"></td>
						<td><input type="reset" value="Reset"></td>
					</tr>
				</table>
			</form>
		</div>

Any help would be appreciated, I have spent hours fiddling with it trying to find an error or a way to get it to work, but I cannot see anything wrong with it, im afraid my Javascript knowledge is very basic.

Recommended Answers

All 5 Replies

forgot to add that all the returns dont really work yet, that was something I was adding in to stop it going to thankyou.html even when it had failed one of the conditions, using a boolean variable.

What do you mean by *doesn't work*? No output, doesn't work as expected?

Use Firefox with the Firebug addon for development which provides a means of debugging Javascript code, inspecting variables etc. If you have any Javascript errors, they would be shown in the Firebug console. Search the web for Firebug tutorials to help you get started with Firebug.

The Alerts don't show, but occasionally have done. The checking length IF function worked fine, then stopped working when I added the IF for checking it was toLowerCase and had none of the unallowed symbols.
But I have also had the LowerCase one work once and I don't know why becuase between it working and not I had changed nothing.

I tried taking steps back by removing IF's back to stages where it had worked but none began working again, so I'm stumped.

I will try that firebug, any other help is muchly appreciated.

It most likely seems to be a Javascript error which is causing your script to execute intermittently. Using Firebug should show you all the errors along with the line numbers; if you still aren't able to understand them, post the exact errors here along with the line numbers.

Also, please consider formatting your code; I can't seem to make anything out of it.

The first thing I would like to suggest to you is to make separate functions to check separate things. It seems like you are checking names, telephone, etc all in the same function. Try to write a function which would call the check functions one by one. If a function returns an error, it would give whatever error message you want. I have done a similar thing in my last semester to validate phone numbers and names, etc. If you are having seriously trouble, I would post my javascript and html here for you to go through.

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.