function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sanchivan SivadasanSanchivan Sivadasan 

inputText prevent postback if less than 2 characters

Hi there,

 

I have an inputText field and a button as follows:

 

<apex:inputText onkeyup="enableDisableGoButton(event);" onkeypress="searchTextEnterPostBack(event);" value="{!searchstring}" id="theSearchstring" maxlength="100" size="50" /> &nbsp;
                                <apex:commandButton value="Go" id="submitButton" styleClass="goButtonDisabled" reRender="theSearchResults" />

 

Here are the Java Script Functions:

 

function enableDisableGoButton(event) {
            var seachInputText = document.getElementById("{!$Component.theSearchstring}");
            var goButton = document.getElementById("{!$Component.submitButton}");
            if(seachInputText.value.length < 2) {
                goButton.className = "goButtonDisabled";
            }
            else {
                goButton.className = "goButtonEnabled";
            }        
        }    
        
        function searchTextEnterPostBack(event) {
            
            var keynum = 0;
        
            if (window.event) {
                keynum = window.event.keyCode;
            }
            else if (e.which) {
                keynum = e.which;
            }
        
            if (keynum == 13) {
                if(seachInputText.value.length < 2) {
                    return null;
                }
                else {
                    return true;
                }
            }
        }

 

So far I have the Go button hide when there is less than two characters on the inputText. That works fine. But when the user clicks enter it does a post back. I want to check if the user pressed enter and if the inputText has less than 2 characters then I don't want the post back to happen if it has atleast 2 characters then the post back should happen as normal. 

 

I am using what is in the inputText as the keyword for <knowledge:articleList>.

 

Please help. Thanks.

 

 

Big VBig V

try this


<apex:form onsubmit="formcheck();">

<apex:inputText onkeyup="enableDisableGoButton(event);" onkeypress="searchTextEnterPostBack(event);" value="{!searchstring}" id="theSearchstring" maxlength="100" size="50" /> &nbsp; <apex:commandButton value="Go" id="submitButton" styleClass="goButtonDisabled" reRender="theSearchResults" />

</apex:form>
function formcheck()
{
var seachInputText = document.getElementById("{!$Component.theSearchstring}");
if(seachInputText.value.length<2)
return false;
else
return true;
}
Sanchivan SivadasanSanchivan Sivadasan

Hi Big V,

 

I tried that and it still posts back. Do you have any other suggestions? Thanks.