+ Start a Discussion
belabela 

i want to show message using javascript which is called when a key entered using inputtext

I wrote a search functionality,i want to show a message "no records found" when there are no records on the entered letter
through a javascript,i got through pagemessage,but i want  through javascript.I tied but its not working,can u please help me regarding this.

<apex:page controller="searchwithactionstatuscontroller" id="one">
<script>
function js(value){
var inputValue = document.getElementById(search).value;
alert(inputValue );
if(inputValue==null||inputValue==''){
alert('no records found');
}
else{
actionFunName();
}
}
</script>

<apex:form id="two">
       <apex:actionFunction name="actionFunName" action="{!searchpositions}"  reRender="two" />
<apex:outputLabel for="search" value="SEARCH :" style="font-weight:bold"   />
<apex:inputText value="{!search}"  id="search" onkeyup="js(value)"   />
<!--<apex:actionSupport event="onkeyup" action="{!searchpositions}" reRender="one:two:block1"/>-->
<apex:outputText id="error" value="{!error}" />
<apex:pageBlock rendered="{!blockOnLoad}" id="block1">
<apex:pageBlockSection >
<apex:pageBlockTable value="{!poslis}" var="pl"   >
<apex:column value="{!pl.name}"/>
</apex:pageBlockTable> 
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock rendered="{!blockOnEnter}" > 
<apex:pageBlockSection id="d">
<apex:pageBlockTable value="{!pos}" var="p"  >
<apex:column value="{!p.name}"/>
<apex:column >
<apex:inputCheckbox value="{!p.name}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
 </apex:pageBlock> 
</apex:form>
</apex:page>


controller
---------------

public with sharing class searchwithactionstatuscontroller {

    public String error { get; set; }
public boolean blockOnLoad{set;get;}
public boolean blockOnEnter{set;get;}


    public PageReference searchpositions() {
        string holdString ='SELECT name from Position__c  where name like  \'%'+search+'%\' ORDER BY name asc';
        pos=Database.query(holdString);
       blockOnLoad=false;
        blockOnEnter=true; 
        return null;
    }
public String search { get; set; }
public List<position__c> poslis{set;get;}
public List<position__c> pos{set;get;}

public searchwithactionstatuscontroller() {
poslis=[select Name from position__c];
blockOnLoad=true;
blockOnEnter=false;
}
}
Best Answer chosen by bela
Vivek DeshmaneVivek Deshmane
Hi ,
Fix js code as below. then it will work. let me know if it works for you.
 
<script>
function js(value){

var inputValue = document.getElementById('one:two:search').value;
alert(inputValue );
if(inputValue==null||inputValue==''){
alert('no records found');
}
else{
actionFunName();

}
</script>

Best Regards,
-Vivek Deshmane

All Answers

Vivek DeshmaneVivek Deshmane
Hi ,
Fix js code as below. then it will work. let me know if it works for you.
 
<script>
function js(value){

var inputValue = document.getElementById('one:two:search').value;
alert(inputValue );
if(inputValue==null||inputValue==''){
alert('no records found');
}
else{
actionFunName();

}
</script>

Best Regards,
-Vivek Deshmane
This was selected as the best answer
belabela
Hi Vivek,
It is working fine,thanks for help.
regarding js i have doubts in above code.pls help me.

1.function js(value)
   <apex:inputText value="{!search}"  id="search" onkeyup="js(value)"   />
   I used parameterized method but the above code is working even i remove parameter value,Is the declaration is correct and when to use parameterized method,because i used as the method returns a value in inputtext through this parameter.Is it correct,if not correct me


2.I am checking database value  in java script [controller:  pos=Database.query(holdString);] when pos contains letter which dont have records ,it should display no records found ,i am trying as i mentioned above in script but i think its not the write way,this i achived through controller, but i want this validation  through javascript 

as i am new to javascript,pls help me to understand.