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
trictric 

Javascript and visual force problem

 

 

 

Hi friend's

,
I have custom field named hello__c  and a dummy controller class named mycontroller1.when the page loads it has hello__c field and a cancel button .All I am trying to do when I click on  cancel button I should be able to show alert to the user  that hello__c field has been left empty.

 

I have created function verify in which I am extracting the value from the hello __c and then I am checking the length of the data in the field .If length is zero then a user should  see an alert box .

 

All the above given things I am trying is not working.Can somebody please help.

 

 

<apex:page controller="mycontroller1">
   
    <apex:form >
    
      <script type ="text/javascript">
  function verify()
  {
  var string;
  string=document.getelementbyid('n').value;
    document.write('the value in the variable s is'+string);
  if(string.length==0)
  {
 
  Alert('U have left field blank');
  return false;
  }
  }
  </script>
     <apex:pageBlock Title="Great">
    <apex:pageBlockButtons >  <apex:commandButton action="{!cancel}" value="Cancel" onclick="return verify()" immediate="true"/></apex:pageBlockButtons>
    <apex:pageblocksection title="Part of greatness">
     <apex:inputField id="N" value="{!account.Hello__c}"/>
 
    </apex:pageblocksection>
    </apex:pageBlock>
    </apex:form>
   </apex:page>

 

 

public with sharing class mycontroller1 {
Account account;
    public PageReference cancel() {
        return null;
    }
    
    public Account getAccount() {
if(account == null) account = new Account();
return account;
}

}

 

 

 

 

kessokesso

Hi!

 

You can achieve this in two ways:

 

1) The easiest, by far, is to just add a Validation Rule. Problem solved. :)

 

2) Alternatively, you can do it via visualforce itself. You can add the <apex:PageMessages /> tag to the page. Then, on the save event of the page you can do your verifiication and return feedback to the user by doing:

 

Apexpages.addMessage ( new Apexpages.Message(ApexPages.Severity.WARNING , "ERROR MESSAGE");

 

(Then make sure to "return null" on the save method)

 

That adds an error message to the "PageMessages" tab on the page.

 

 

 

3) A bit harder, instead of "onclick = 'return verify()'" you should add an <apex:actionsupport> tag (http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_actionSupport.htm) that calls your javascript.

 

PS: Best Practice is Option 1 or 2, since it also provides the user with the same type of feedback that all other Salesforce pages provide for errors.

RizwiRizwi

The problem is you cant use visualforce ids for javascript getelementbyIdFunction

 

id for you input field will look something like following

j1_01:gf_rt:.....:N

 

you have to use this for javascript

 

use your browser feattures to identify this id pattern. You can use chrome browser, or firebug with mozilla firefox fro this.

 

ask if you need more help

 

 

trictric

Hi Keso,

 

Thanks, a lot keso

 

I understand that I can use it a validation rule but i do not want to use as earlier I intneded to use Onblur event so when user doesn't put any value in the text field and he tries to move to the next field .He should see an alert .Since OnBlur wasn't working therfore I changed onblur with another event to see if it's works unfortunatelt it is not working.

 

I would really appreciate If you have some advice or solution on how to use javascript here as I will be using it frequently.

 

Thanks,

Nitin sharma

 

 

 

 

 

trictric

Thanks a lot Rizwi.

 

 

in the end you said ask for more help if needed.Here I come,I do not understand when you say we need not pass inputfield ID to the javascript.

 

Then how do we do that.Do you have some example which can demonstarte this more clearly.I am using fireforx browser for this.

 

 

Thanks,

trick

RizwiRizwi

first thing is we cant access visualforce components with javascript using id we put there.

for a example if we had following structure

<apex:page id="mypage">

 <apex:form id="myform">

<apex:inputtext id="myinput"/>

</apex:form>

</apex:page>

 

when you have to use id as "mypage:myform:myinput" to acces with javascript.

OR

you can use standard method

document.getElementById(“{!$Component.myinput}”);

 

Here $component means where you are now. which mean in this case im writing the script inside the form tag.

 

you may find something important here

http://wiki.developerforce.com/page/Using_Browser_Technologies_in_Visualforce_-_Part_2

aballardaballard

ALWAYS use $Component.id to get the browser Dom ID that corresponds to your visualforce component id.   That way it will continue to work if you make changes to your page that cause the dom id to change  (or if Salesforce makes internal changes that result in a different dom id.)

 

Reverse-engineering by viewing the generated html is never a good idea. 

kessokesso

I would then recommend  a bit of jQuery magic here to make your life easy. (like assigning all components a specific function to run onblur)

 

However, to access components within Javascript, you should use the $Component object or go completely crazy figuring out the exact IDs of your elements. Remember that you can also pass the object "this" to a function and use that instead.