This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes HTML, CSS and JavaScript and the fly likes Problem with Form Validation. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Problem with Form Validation." Watch "Problem with Form Validation." New topic
Author

Problem with Form Validation.

Gabriel Fox
Ranch Hand

Joined: Oct 17, 2001
Posts: 170

I have three buttons(input type="submit") on my LogOn Screen
which expects a user to input the name and password in these 2 textboxes provided for authentication.
The buttons are 1.see home page button
2.see change password screen button
3.exit logon screen button
I am checking for empty fields (in username and password fields)
if any popup alert(message).But the checking of empty fields
should not happen on clicking the exit button.It should happen
only when the button 1 or button 2 is clicked.
the exit button should be handled by the action ="/servlet/Controllerservlet"
By help, Codes are welcomed.Thanks.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Gabriel
It looks like all three of your buttons are of the same format, ie they look like this:
<input type=submit value=xxx>
If thats true then anytime you click on one of the buttons the form is submitted. To change the functionality of one of the buttons make a plain button then add the functionality to the onclick method. This might work for your exit button:
<input type=button value='Exit Logon' onclick='window.location("/servlet/Controllerservlet")>
And then something similiar for your 'see password change screen' button.
to check for empty fields, this code works well:

Just pass it the string your checking and it returns true or false.
hope that helps
Dave


Dave
Gabriel Fox
Ranch Hand

Joined: Oct 17, 2001
Posts: 170
Thanks Dave for your prompt response.But, I am still having problems with this validation. I am trying all methods ,but as A BEGINNER to JavaScript i'm not finding it easy.
What i have at the moment is the following code in my JSP
: see below
<html>
<head>

<SCRIPT LANGUAGE="JavaScript">
function checkrequired(which)
{
var pass=true;
for (i=0; i < which.length ; i++)
{
var tempobj=which.elements[i];
if (tempobj.name.substring(0,8)=="required")
{
if((tempobj.type=="text"| |tempobj.type=="password")&& tempobj.value=='')
{
pass=false;
break;
}
}
}
if (!pass)
{
alert("Please make sure all fields are properly completed.");
return false;
}
else
{
return true;
}
}
</SCRIPT>
</head>
<Form name="formName" action="/servlet/ControllerServlet" method="Post" onSubmit='return checkrequired(this)' >
<CENTER>
<table bgColor="white">
<tr>
<td> User : </td>
<td> <input type="text" name="requiredusername" ></td>
<td> <input type="submit" name="ok" value="ENTER"> <td>

</tr>
<tr>
<td > Password : </td>
<td > <input type="password" name="requiredpassword" value="password"> </td>
<td > <input type="submit" name="changePassword" value="CHANGE PASSWORD" > </td> <BR>

</tr>
<tr>
<input type='button' name='exit' onclick='window.location("/servlet/ControllerServlet");' value="E X I T " </td>
</tr>

</table>
</form>
</html>
Please, an urgent response will be appreciated.
Note:
Dave, i donot really know the blank checker function will
be used. Besides, aside from the Logon screen i have loads of
pages(JSP) which i need to validate and this pages have say
minimum 10 fields(compulsory) and few optional ones.
So , i need to get the validation concept very clear.

Question.
At the moment using the above script pops up the alert on
clicking the the ENTER and CHANGE-PASSWORD button + empty fields,
Which am happy with.But , on clicking the EXIT button,It pops up
a runtime error ,saying "Property or function CANNOT BE USED ON THIS OBJECT".I am confused.
NOTE: Is it possible to go straight to the "ExitPage.jsp" without
going through the form handler in the action ="controllerServlet".If YES,Pls
how is it done.
Please help!!!
Codes are very welcomed.Thanks.


[This message has been edited by Gabriel Fox (edited November 15, 2001).]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Gabriel
Two things, first, I apologize - I gave you the wrong fnction for changing the location of the window it should be:
onclick='window.location="/servlet/ControllerServlet";'
Here also is the rest of your javascript function, it works ok, but you had a couple of typos in your if statement (had some extra spaces in between the | |.

For your exit page you can just enter the URL:
window.location = "exitPAge.jsp"; And you'll go right to it, I'm not that familiar with servlets yet so I'm not sure what the controllerServlet does.
Hope this helps
Dave
Gabriel Fox
Ranch Hand

Joined: Oct 17, 2001
Posts: 170

Nice one !!! You made my Day again Dave .Cheers!!!
I saw the window.loaction=----- while struggling with my
JScript book ,but i was not too ready to read more on it's use. Instead,
i used the :
onclick="location.replace('NameOfPage.jsp');" which took
me straight to the NameOfPage.jsp WITHOUT GOING THRU the
action="FormHandler"(that worked fine).
NOTE: I am developing an application based on
Java Technology(JSPs,Servlets,JavaBeans) using model-2 framework
and i realised i need enough front end processing (e.g form validation etc) to make life easier.
After testing the posted script : i confirmed an invalid user can go pass the page(not necessarily logon) using just a space character or this character: `
in either textfields.
Question:1 How do we guard against going pass the logon form (screen) using any strange character aside from whitespace i.e.' '
2. Can you please suggest the JScript standard validation
technique for a from with say 10 required fields and 4 optional
ones.somes of the fields are :address lines 1-4,phone,fax,
e-mail,amount(currency),pain text.
Codes will be highly appreciated as well any nice source-code
website. Cheers

Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Gabriel
Here is a little piece of code that will check if a string is all whitspace, you can easily change it to check for strange characters too.

To actually guard against someone going past the login screen is rather difficult with javascript. you can make sure they dont enter just a blank or strang echaracters. You cant verify the password and user name until they get to the next page where you check them against the database or where-ever you keep them.
You can do with Javascript is rudimentary validation on the client.
If you want to do more high-level validation pof what they enter then just do it on the jsp or servlet page they submit to and then if it wrong return them to an error page.
The way I normally do it is to check for obvious incorrect entries with javascript, something like:

To do this I start all of the required fields with an r, that way you dont bother checking things like the submit button and other things.
hope that helps you out
Dave

Gabriel Fox
Ranch Hand

Joined: Oct 17, 2001
Posts: 170
Thanks Dave i will get back as soon possible!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with Form Validation.