[thelist] [Javascript] Form validation with regex

Rich Gray richardgray at onetel.co.uk
Wed Oct 30 04:13:01 CST 2002


I'm a bit late on this thread - maybe you've got this sorted already...

->	surnamefield.match($surnamepattern))

You're using php syntax for the surnamepattern var... does it work if you
drop the '$'?


-----Original Message-----
From: thelist-admin at lists.evolt.org
[mailto:thelist-admin at lists.evolt.org]On Behalf Of Garrett Coakley
Sent: 29 October 2002 14:02
To: thelist at lists.evolt.org
Subject: [thelist] [Javascript] Form validation with regex

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)

For unsubscribe and other options, including
the Tip Harvester and archive of thelist go to:
http://lists.evolt.org Workers of the Web, evolt !

"The information contained in or attached to this email is intended only for
the use of the individual or entity to which it is addressed. If you are not
the intended recipient, or a person responsible for delivering it to the
intended recipient, you are not authorised to and must not disclose, copy,
distribute, or retain this message or any part of it. It may contain
information which is confidential and/or covered by legal professional or
other privilege (or other rules or laws with similar effect in jurisdictions
outside England and Wales).

The views expressed in this email are not necessarily the views of Centrica
plc, or Centrica Telecommunications Limited, and their directors, officers
or employees make no representation or accept any liability for its accuracy
or completeness unless expressly stated to the contrary."

More information about the thelist mailing list