*
The moose likes HTML, CSS and JavaScript and the fly likes Problems with form validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Problems with form validation" Watch "Problems with form validation" New topic
Author

Problems with form validation

sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43
I have a html form ,where I have text fields and select box .now I want to do the following:
If any of the fields is null then an error message will be shown beside the fields which are null(and not on a alert box) .How will I do it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Check the fields in a submit handler for the form. If there's a problem, show the errors and return false to stop the form submission.

Start the code and we'll take it from there.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43
I did the following


But How will I print an error message right next to the text box and select box(if any of them is null)?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Please use better indentation in your code.

Also using references such as document.forms["f1"]["fl_no"] for DOM elements is old-fashioned and fragile. A more modern approach is to assign id values to the elements and use document.getElementById().

To create the error messages you can create DOM elements on the fly and inject them into the DOM at the appropriate places.

All of that would be far easier using a library such as jQuery to help you.

(In fact, the jQuery validation plugin handles it all for you and would be far less work.)
sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43
I started using the jquery validation plugin but I am facing the folloe=wing problems
1)I have 1 text field("flno") and 2 select boxes("src" and "dest") ,now if I select the "flno " I dont need to use the other select boxes ,else the select boxes become mandatory.
2)What shall I do to check that the values in select boxes are never equal,is there something like notequalTo rule?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

For something that specific you'd like need to write a custom rule function for the validator.
Ayan Biswas
Ranch Hand

Joined: Jul 10, 2010
Posts: 104
For something that specific you'd like need to write a custom rule function for the validator.

1)I wrote a custom method to check the value of to fields should not be equal
and then I add this method to rule of field2 element ...but it does not work!!
2) I tried using required( dependency-callback ) to check that if a textfield value is not selected then a selectbox field would become mandatory,else it would be optional,so I used the following and this does not work as well
3)Is it mandatory that the name and id field of the input boxes should be same to get it validated?


AyanBiswas
sumit jagtap
Greenhorn

Joined: Dec 08, 2011
Posts: 26
When you find the null parameter after that check your database field, it will be unmarked to not null
Malini Rao
Greenhorn

Joined: Mar 30, 2012
Posts: 10
TRY:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with form validation
 
Similar Threads
null field value on submit
submit button + selecting content of field automatically
Checking input values in form with JQuery
Validation in JSP.
Simple example of passing a variable between 2 forms