验证VF页面上所需字段输入的Javascript不起作用

Javascript to validate required field entry on a VF page not working

本文关键字:输入 字段 Javascript 不起作用 VF 验证      更新时间:2023-09-26

我正在尝试为我的visualforce页面添加一些javascript,该页面也是force.com网站。

我尝试了几种不同的方法来获得一个字段是弹出的必需消息,但它不起作用。我粘贴了下面的代码,如果有任何帮助,我们将不胜感激。

<apex:form >
<script>
function doValidation(){
      var checkvalu = <apex:outputText value="{!customer}"/>;
      if(checkValu.length >0){
         saveNow();
      }
      else{
        alert('Please fill out all fields!');
     }
}
</script>
        <apex:panelGrid styleClass="section">Please enter your contact information</apex:panelGrid>
        <apex:panelGrid styleClass="survey">Name</apex:panelGrid>
        <apex:inputText styleClass="textarea" value="{!customer}"/>

        <apex:panelGrid styleClass="survey">Company</apex:panelGrid>
        <apex:inputText styleClass="textarea" value="{!company}"/>

        <apex:panelGrid styleClass="survey">Address</apex:panelGrid>
        <apex:inputText styleClass="textarea" value="{!Address}"/>

        <apex:panelGrid styleClass="survey">City/Town</apex:panelGrid>
        <apex:inputText styleClass="textarea" value="{!city}"/>

        <apex:panelGrid styleClass="survey">State/Province</apex:panelGrid>
        <apex:inputText styleClass="textarea" value="{!state}"/>

        <apex:panelGrid styleClass="survey">Zip/Postal Code</apex:panelGrid>
        <apex:inputText styleClass="textarea" value="{!zip}"/>

        <apex:panelGrid styleClass="survey">Country</apex:panelGrid>
        <apex:inputText styleClass="textarea" value="{!country}"/>

        <apex:panelGrid styleClass="survey">Email Address</apex:panelGrid>
        <apex:inputText styleClass="textarea" value="{!email}"/>

        <apex:panelGrid styleClass="survey">Phone Number</apex:panelGrid>
        <apex:inputText styleClass="textarea" value="{!phone}"/>

您可以使用特定组件的必需属性直接在Visualforce中根据需要创建字段

<apex:inputField value="{!Account.Name}" required="true"/>

要在VisualForce页面中使用Javascript制作所需字段,请

<apex:page StandardController="Account">
    <script>
        function setRequired() {
            if (document
                    .getElementById('{!$Component.theForm.thePageBlock.thePageBlockSection.accPhone}').value == '')
                alert("Phone number will not be blank");
        }
    </script>
    <apex:form id="theForm">
        <apex:pageBlock id="thePageBlock">
            <apex:pageBlockButtons>
                <apex:commandButton value="submit" action="{!save}"
                    onclick="setRequired();" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection id="thePageBlockSection">
                <apex:inputField id="accPhone" value="{!Account.Phone}" />
                <apex:inputField value="{!Account.Name}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

谨致问候,Naveen软件工程师http://www.autorabit.com