1

I want to do something like the following in a visualforce page

<apex:inputTextarea id="abc" 
        value="if('abc'.value().size()>0,alert('Hi'),{!AModel.Comment.Body}"

Can anyone tell me how to achieve this

I want to display an alert if the entered value is null. else I want to pass it to the controller.

  • why don't you just check the AModel.Comment.Body this is null or not in your controller and show error message. else when you try to submit/save the page. before calling the controller method, call a javascript function there check this field is empty or not. and based on that show alert message – Ratan Paul Jun 16 '16 at 12:06
  • That is not possible actually... The error message has to come in an alert box. – Rimii Jun 16 '16 at 12:12
  • what is not possible? the controller validation or javascript alert message? – Ratan Paul Jun 16 '16 at 12:13
  • It has to be a javascript alert message. It cant be a controller validation. – Rimii Jun 16 '16 at 12:14
  • 1
    Use the Ratan's code of Javascript in a Function and Call that function from the Onclick of Command Button... – Subhash Jun 16 '16 at 12:28
0

You can add a javascript validation like below

<apex:inputTextarea id="abc" 
        value="{!AModel.Comment.Body}" />

in javascript

var commentBodyVal = document.getElementById('{!$Component.abc}').value;
if(commentBodyVal.trim().length == 0 )
{
    alert('Your text area field is empty. Please add something.');
}
else {
   //call action function to call your controller method
}
  • document.getElementById('{!$Component.abc}').value statement is throwing an error.. it it saying cannot read the .value property of null @Ratan – Rimii Jun 20 '16 at 12:11
  • @Rimii may be your component is inside multiple VF tags that s why it is not able to access .. check this doc developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… – Ratan Paul Jun 20 '16 at 12:15
  • @Rimii your id may be like this {!$Component.theBlock.theSection.theSectionItem.theText} multi component ids – Ratan Paul Jun 20 '16 at 12:21
  • @Rimii is tis question still open ? – Ratan Paul Jun 29 '16 at 6:46
  • no the question is not open – Rimii Jun 29 '16 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.