Javascript 我想显示警报(验证表单)和don';如果表单在portlet liferay中无效,则无法提交

Javascript 我想显示警报(验证表单)和don';如果表单在portlet liferay中无效,则无法提交,javascript,validation,liferay,portlet,liferay-aui,Javascript,Validation,Liferay,Portlet,Liferay Aui,我在liferay 6的portlet中有一个表单。我想在从表单发送数据之前验证数据,但我不能。我希望如果我的字段为空,则表单不会提交 我的jsp代码: <script> function submitForm18() { var pre=document.forms['<portlet:namespace/>myFormpostcode']['<portlet:namespace/>pre'].value; if (pre == "" ||

我在liferay 6的portlet中有一个表单。我想在从表单发送数据之前验证数据,但我不能。我希望如果我的字段为空,则表单不会提交

我的jsp代码:

<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>

函数submitForm18(){
var pre=document.forms['myFormpostcode']['pre'].value;
如果(pre==“”| pre==null){
警惕(“错误”);
表单['myFormpostcode']['pre'].focus();
返回false;
}
document.forms['myFormpostcode'].submit();
}  
:  
如果字段为空,此代码将显示警报并提交表单,但如果表单数据无效,则我希望显示警报(验证表单)并不提交。

可能尝试添加如下“else”语句:

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]
   }
}
函数submitForm18(){
[b] var pre=document.forms['myFormpostcode']['pre'].value;
如果(pre==“”| pre==null){
警惕(“错误”);
表单['myFormpostcode']['pre'].focus();
返回false;
}否则{/*这是新的*/
document.forms['myFormpostcode'].submit();[/b]
}
}

在第9行,您使用的是
表格[

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

这也许可以解释为什么要提交表单,因为它在第9行是一个javascript错误,并且没有返回
false

为什么不在提交之前尝试使用aui表单验证程序验证表单字段

你的验证值是多少?你有文本框吗?对不起,我编辑了我的代码,这是一个很好的问题,但请下次格式化你的帖子,不要只是复制粘贴。这些问题对你有帮助吗?如果有,请接受正确的问题或你看到的最好的问题
document.forms['<portlet:namespace/>myFormpostcode']['<portlet:namespace/>pre'].focus();