View Full Version : Help needed with form validation
uk_martin
07-19-2004, 07:20 AM
Hello, and HELP! (please)
I have an application form which I have nearly finished, and can be seen here
Click here to see the form (http://www.ukmartin.com/form.htm)
As you will see, the form is on the right, with the source code on the left.
At present, the form works with the exception of the UserName and Password fields. The form will detect wheter or not any characters have been typed in, but what I need is for the script to check if there are at least 6 characters in each. Ideally, checking that the 2 password entries are the same would be a bonus.
I cannot write JavaScript, but I have got the form to where it is by copying / pasting publicly available scripts and applying a bit of logic to some minor editing. This bit about checking for 6 characters is beyond me though.
Any help in finalising this project would be much appreciated.
Thanks
Martin
Willy Duitt
07-19-2004, 07:38 AM
What's this? // Note: doesn't use regular expressions to avoid early Mac browser bugs
Is this truly a concern of yours or only part of the original script.
If not, and I can use regular expressions I could probably work something up for you to calidate those two fields...
.....Willy
Willy Duitt
07-19-2004, 08:01 AM
Here's something I had lying around that does not use regular expressions...
(except for the email verify but you didn't ask for that and you can just remove it when you work these codes into yours)
<script type="text/javascript">
<!--//
function validate(form){
var goodEmail = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(form.email.value);
if(!form.email.value=="" && !goodEmail){
alert('Please enter a valid email address.');
form.email.value = '';
form.email.focus();
return false;
}
for(var i=0; i<form.elements.length; i++){
if(form.elements[i].value==""){
alert('All fields must be completed!');
form.elements[i].focus();
return false;
}
}
if(form.opass.value == form.npass.value || form.npass.value.length < 6){
alert('The new password must at least 6 characters\nand not match the old password!');
form.npass.value='';
form.rnpass.value='';
form.npass.focus();
return false;
}
if(form.npass.value != form.rnpass.value){
alert('New passwords do not match, try again!');
form.npass.value='';
form.rnpass.value='';
form.npass.focus();
return false;
} return true;
}
//-->
</script>
</head>
<body>
<form method="get" action="" onsubmit="return validate(this)">
Email address: <input type="text" name="email"><br>
Old password: <input type="password" name="opass"><br>
New password: <input type="password" name="npass"><br>
Re-enter new password: <input type="password" name="rnpass"><br>
<input type="submit" value="Submit">
</form>
.....Willy
uk_martin
07-19-2004, 05:28 PM
What's this? // Note: doesn't use regular expressions to avoid early Mac browser bugs
Is this truly a concern of yours or only part of the original script.
If not, and I can use regular expressions I could probably work something up for you to calidate those two fields...
.....Willy
This was in the original script - far to advanced for me to know what it means or what the consequences would be to delete it, so as it didn't need feeding or watering and wasn't a nuisance (I hope) I left it there.
uk_martin
07-19-2004, 07:25 PM
Hi again Willy
I've been able to adapt the script in your second reply to do exactly what I want (& I've dumped the original)
Thanks for your help :)
Martin
Basscyst
07-19-2004, 08:01 PM
I realize you have this resolved, but just for some practice I validated your form with regular expressions, it gives you a bit more than you asked for, but I was bored. I would appreciate some feedback if someone spots somthing I have done wrong in my RegExps as I am still learning this. Seems to work though.
Here ya go!
<html>
<head>
<style type="text/css">
table
{
text-align:left;
width:300px;
}
</style>
<script type="text/javascript">
function formValid()
{
var stat=0;
for(i=0;i<document.forms[0].length;i++)
{
var obj=document.forms[0].elements[i];
obj.style.borderColor='#eba31d';
if(i==0||i==1)
{
var re=/^[a-zA-Z]+\s[a-zA-Z]+$/
if(obj.value.search(re)==-1)
{
obj.style.borderColor='red';
stat=1;
}
}
if(i==4||i==5||i==6)
{
var re=/^[\da-zA-Z]{6,}$/
if(obj.value.search(re)==-1)
{
obj.style.borderColor='red';
stat=1;
}
}
switch(i)
{
case 2:
var re=/^\d+$/
if(obj.value.search(re)==-1)
{
obj.style.borderColor='red';
stat=1;
}
break;
case 3:
var re=/^\d{1,2}\/\d{1,2}\/\d{4}/
if(obj.value.search(re)==-1)
{
obj.style.borderColor='red';
stat=1;
}
break;
case 7:
var re=/^[\w\.]+@[a-zA-Z]+\.[a-zA-Z]+$/
if(obj.value.search(re)==-1)
{
obj.style.borderColor='red';
stat=1;
}
break;
}
}
if(stat==1)
{
alert("Please fill out the form completley, missing fields are marked in red.")
}
if(document.forms[0].elements[5].value!=document.forms[0].elements[6].value)
{
alert("The password fields do not match! Please correct this and try again!");
}
}
</script>
</head>
<body bgcolor="#eba31d" leftmargin="5" topmargin="0">
<form>
<table>
<thead>
<th colspan="2">Not yet a member?</th>
</thead>
<tr>
<td colspan="2">Apply for membership here. Please complete
and submit this form to us. We will endeavour
to send you a password once your details have
been checked.</td>
</tr>
<tr>
<td class="ttl">1st Persons Name:</td>
<td><input type="text" id="nm1" name="nm1" /></td>
</tr>
<tr>
<td class="ttl">2nd Person's Name:</td>
<td><input type="text" id="nm2" name="nm2" /></td>
</tr>
<tr>
<td class="ttl">Membership Number:</td>
<td><input type="text" id="mbr" name="mbr" /></td>
</tr>
<tr>
<td class="ttl">Expiry Date:</td>
<td><input type="text" value="mm/dd/yyyy" id="xpr" name="mbr" /></td>
</tr>
<tr>
<td colspan="2">Choose your Usernames and passwords - but remember,
they must be at least 6 characters long, and contain
no spaces. </td>
</tr>
<tr>
<td class="ttl">Desired Username:</td>
<td><input type="text" id="usenm" name="usenm" /></td>
</tr>
<tr>
<td class="ttl">Password:</td>
<td><input type="password" id="pw1" name="pw1" /></td>
</tr>
<tr>
<td class="ttl">Re-type Password:</td>
<td><input type="password" id="pw2" name="pw2" /></td>
</tr>
<tr>
<td class="ttl">e-Mail Address:</td>
<td><input type="text" id="eml" name="em1" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="Submit" onclick="formValid()" /></td>
</tr>
</table>
</form>
</body>
</html>
Basscyst
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.