2

I have a form in my portlet in liferay 6. I want to validate data before sending data from form, but I cannot. I want that if my field is empty, the form does not submit.

My jsp code:

<script>
function submitForm18() {
    var pre=document.forms['<portlet:namespace/>myFormpostcode']['<portlet:namespace/>pre'].value;
    if (pre == "" || pre == null) {
        alert("errrror.");
        forms['<portlet:namespace/>myFormpostcode']['<portlet:namespace/>pre'].focus();
        return false;
    }
    document.forms['<portlet:namespace/>myFormpostcode'].submit();
}  
</script>

<aui:form action="<%= myUrl%>" method="post" name="myFormpostcode" id="send_info" onsubmit="return submitForm18(); return false;">
    <liferay-ui:message key="pre-code" />    :  <liferay-ui:message key="without-zero" />
    <aui:input size="4" maxlength="4" name="pre" type="text" label=""></aui:input> 
    <aui:button type="submit" value="send" name="KeyNB" cssClass="buttom-submit" />               
</aui:form>

This code shows alert if field is empty and submits form but I want to show alert(validate form) and don't submit if form data is not valid.

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
maryam
  • 584
  • 9
  • 26

3 Answers3

1

Why dont you try aui-form-validator to validate your form fields before submitting http://alloyui.com/tutorials/form-validator/

Pritesh Shah
  • 857
  • 1
  • 7
  • 8
0

maybe try to add an "else" statement like so:

function submitForm18() {

   [b]var pre=document.forms['<portlet:namespace/>myFormpostcode']['<portlet:namespace/>pre'].value;
   if (pre == "" || pre==null ) {
       alert("errrror.");
       forms['<portlet:namespace/>myFormpostcode']['<portlet:namespace/>pre'].focus();
       return false;
   } else { /* this is new */
       document.forms['<portlet:namespace/>myFormpostcode'].submit();[/b]
   }
}
Kreutzer
  • 328
  • 4
  • 12
0

At line No. 9 you are using forms[

forms['<portlet:namespace/>myFormpostcode']['<portlet:namespace/>pre'].focus();

instead of document.forms[.

document.forms['<portlet:namespace/>myFormpostcode']['<portlet:namespace/>pre'].focus();

That might explain why the form is submitted, since it is a javascript error @line 9 and is not returning false.

Prakash K
  • 11,669
  • 6
  • 51
  • 109