[thelist] [Javascript] Form validation with regex

Garrett Coakley garrett at polytechnic.co.uk
Tue Oct 29 08:03:01 CST 2002

Hash: SHA1

Hi all,

Bit of background:

We have a new client with a legacy site that we're rebuilding for them,
but they've highlighted a small problem with user input on one of their
existing forms that they would like a quick fix for.

The form is the standard Title, Initial, Surname, Address thing, which
gets fed directly into their backoffice system. Unfortunately, the
software they are using has huge problems with non alphanumeric
characters and the Surname field. It falls over if it receives one.

Yesterday someone put an email address in the Surname field, and their
backoffice system barfed on the '@'.

I've been tasked with adding some JS validation to the Surname field to
ensure this doesn't happen again. I think I've got the regex sorted, but
it's not working for some reason, and JS is not my thing at all, so I'm
a bit stuck.

This is the relevant bit of the form:

	<form name="userdetails" action="http://aurlsomewhere" method="post"
	onSubmit="return confirmValid();">
	<input TYPE="text" SIZE="20" NAME="surname" VALUE="">

And this is the first part of the confirmValid() function:

	function confirmValid() {
	if ((document.userdetails.surname.value)==""){
		alert('Please enter your surname'); return false;

As you can see, they already have a check in place to ensure the field
isn't left empty, so this is what I added to the function to get the
character check in place:

	var surnamepattern = /^[a-zA-Z\-\']+$/;
 	var surnamefield = document.userdetails.surname.value ;
	function confirmValid() {
     	if ((document.userdetails.surname.value)=="" ||
		alert('Please enter your surname'); return false;

But, surprise surprise, it's not working.

I tried to use Venkman (the Mozilla Javascript debugger) to see what the
problem is, and it comes back with:

	X-Error: surnamefield has no properties

I'm sure it's something obvious I'm missing, but for the life of me I
can't see what it is.

Any ideas?


Version: GnuPG v1.2.0 (GNU/Linux)


More information about the thelist mailing list