1

Below is the javascript on my JSP page:

<SCRIPT language="JavaScript">
function checkPass(){

var pass1=request.getParameter('password');
var pass2=request.getParameter('password2');

    if (pass1==pass2){
        window.alert("YES");
    }
    else{
        window.alert("NO");
    }
    }
</SCRIPT>

Below is my form input text boxes:

                    <h:outputLabel value="Password:" for="password" />
 <h:inputSecret id="password" value="#{StaffController.staff.password}" 
            size="20" required="true"
            label="Password" >
            <f:validateLength minimum="8" />
        </h:inputSecret>

        <h:message for="password" style="color:red" />

                    <h:outputLabel value="Password:" for="password2" />
 <h:inputSecret id="password2" 
            size="20" required="true"
            label="Password" >
            <f:validateLength minimum="8" />
        </h:inputSecret>
 <h:message for="password2" style="color:red" />

Below is my commandbutton linked with onmousedown to call the script when clicked:

<h:commandButton action="#{StaffController.saveUser()}" onmousedown="checkPass();" value="Submit"/>

I can't seem to retrieve value from my user input form.

RUiHAO
  • 19
  • 3
  • 7

2 Answers2

2
  1. JSF changes id of components - view source of your page in browser. It should be something like a combination of form id+component id

  2. Submit command invokes onmousedown event before making submit, so in checkPass you do not have a request object

  3. Better to write a custom validator for checking password

corsair
  • 648
  • 5
  • 14
-1

First you require to set in the h:form the value false to the "prependId" attribute to make possible to get the textboxes by id on javascript. Is not recomended to remove the prependId. But it makes to you easier to work with javascript. In other way you could use a field name or css name an other kind of selection (like using Jquery $('.cssname') or $('#cssid')

Now you need to change the javascript. javascript is performed on the client side. You can't get data from the request because the request is not done when the javascript is executed.

You need to get the inputSecret object ,that in the html is an input item, using something like this:

var pass1 = document.getById('password').value;
var pass2 = document.getById('password2').value;
Dubas
  • 2,605
  • 1
  • 20
  • 34
  • so it will most probably work after i change this two lines, and nothing else? what do you mean by changing the h:form the value false to "prependld"? – RUiHAO Aug 17 '11 at 07:59
  • if you don not add to the tag (that needs to contain the entire form ) the prependId="false" on your jsf the id's of the elements in the resultant HTML code are defined using an automated process that makes unpredictable id's instead of the ones you are added to the controls. You can view it reviewing the html source code of the final page in your navigator. – Dubas Aug 17 '11 at 08:03
  • so the h:form tag should cover my script all the way till my commandbutton? – RUiHAO Aug 17 '11 at 08:07
  • i have added from the beginning of my script, all the way down after my commandbutton . However the form still runs without doing comparison – RUiHAO Aug 17 '11 at 08:10
  • Downvoted. DON'T add the `prependId="false"`, bad choice: https://stackoverflow.com/questions/7415230/uiform-with-prependid-false-breaks-fajax-render, just read https://stackoverflow.com/questions/8634156/how-to-find-out-client-id-of-component-for-ajax-update-render-cannot-find-compo and learn how to get the right id: https://stackoverflow.com/questions/8634156/how-to-find-out-client-id-of-component-for-ajax-update-render-cannot-find-compo – Kukeltje Nov 05 '18 at 11:00