This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes JSF and the fly likes jsf -javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "jsf -javascript" Watch "jsf -javascript" New topic

jsf -javascript

vishal gaikwad

Joined: Oct 01, 2011
Posts: 11
i want to show message for all required fields after submiting the form what should i do?? is it the right way??

<script language="JavaScript">

function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("FirstName", "LastName");
// Enter field description to appear in the dialog box
var fieldDescription = Array("First Name", "Last Name");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
if (!blnchecked){
alertMsg += " - " + fieldDescription[i] + "\n";

if (alertMsg.length == l_Msg){
return true;
return false;
// -->

<!--SAMPLE FORM -------------------------------->
<form name="formcheck" onsubmit="return formCheck(this);">
First Name: <input type=text name="FirstName" size="25"><br>
Last Name: <input type=text name="LastName" size="25"><br>
<input type=submit value="Submit Form">
Hussein Baghdadi
clojure forum advocate

Joined: Nov 08, 2003
Posts: 3479

Hi and welcome to the Ranch ,
Please edit your post and use "Code" tags to format your code, it is hard to read your code right now without formatting.

Where is the JSF part in your code? Your sample form is pure HTML.
Guy deLyonesse
Ranch Hand

Joined: Apr 12, 2011
Posts: 200

With JSF's built-in validation feature there is absolutely no reason whatsoever to use JavaScript for validation. If you are indeed using JSF then you should find a good resource for learning JSF and review the Validation features.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: jsf -javascript
It's not a secret anymore!