...

View Full Version : Form validation (Java)



Pacman1
02-26-2007, 03:22 PM
I am very new to JAVA but have some experience in HTML. For learning experience, I simply want to validate a form. Would really appreciate your advice and your example. If possible, the script with comments so I know what is going on.


Here is my form:
<FORM METHOD="POST"
ACTION="mailto:szather@ouray.com"
ENCTYPE="text/plain"
onSubmit="return valid(this);">

<strong>Your Email:</strong>
<input type="text" name="email">
<br>
<strong>Date:</strong>
<input type="text" name="date">
<br>
<strong>Time:</strong>
<input type="text" name="time">
<strong><br>
<br>
Please write the issue:</strong><br>
<textarea name="problem" COLS="80" ROWS="10"></textarea>
<br>
<br>
<INPUT TYPE="submit" VALUE="Submit">
</FORM>


The validation:
- Email address = the email address has an "@" and a "." following the "@" sign.
- Date = date is entered in the format 03/15/2007
- Time = e.g. in the form 3:35 PM or 11:02 AM
- Please write the issue = problem description is at least 2 characters long

<SCRIPT> LANGUAGE="JavaScript"> I believe is a good start.. (help please).. Any example of a very simple script, with comments if possible. I checked the script on many sites, and I am still confused with the code. Thanks for any help..

chump2877
02-26-2007, 04:35 PM
First of all, welcome to Coding Forums...:)

Second, Java and Javascript are 2 entirely different scripting languages....This is a Javascript forum, so I am offering you a Javascript solution...

Please study the following code and try to learn something from it....generally, I usually only help people who have made an effort in scripting their own javascript.

The bulk of the form validation I acheived with regular expressions, as follows:


<FORM METHOD="POST"
ACTION="mailto:szather@ouray.com"
ENCTYPE="text/plain"
name="form1"
onSubmit="return valid(document.form1);">

<strong>Your Email:</strong>
<input type="text" name="email">
<br>
<strong>Date:</strong>
<input type="text" name="date">
<br>
<strong>Time:</strong>
<input type="text" name="time">
<strong><br>
<br>
Please write the issue:</strong><br>
<textarea name="problem" COLS="80" ROWS="10"></textarea>
<br>
<br>
<INPUT TYPE="submit" VALUE="Submit">
</FORM>


<script type="text/javascript">

function valid(formObj)
{
if (formObj.email.value.length > 0)
{
var myRegxp = /(.+?)(@)(.+?)(\.)(.+?)/;
if (!myRegxp.test(formObj.email.value))
{
alert('Email was entered incorrectly');
formObj.email.focus();
return false;
}
}
else
{
alert('Email was entered incorrectly');
formObj.email.focus();
return false;
}

if (formObj.date.value.length > 0)
{
var myRegxp2 = /(\d{2})(\/)(\d{2})(\/)(\d{4})/;
if (!myRegxp2.test(formObj.date.value))
{
alert('Date was entered incorrectly');
formObj.date.focus();
return false;
}
}
else
{
alert('Date was entered incorrectly');
formObj.date.focus();
return false;
}

if (formObj.time.value.length > 0)
{
var myRegxp3 = /(\d{1,2})(:)(\d{2})( )(AM|PM)/;
if (!myRegxp3.test(formObj.time.value))
{
alert('Time was entered incorrectly');
formObj.time.focus();
return false;
}
}
else
{
alert('Time was entered incorrectly');
formObj.time.focus();
return false;
}

if (formObj.problem.value.length < 2)
{
alert('Problem was entered incorrectly');
formObj.problem.focus();
return false;
}

return true;
}

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum