+ Start a Discussion
jagjitsingh@indivar.comjagjitsingh@indivar.com 

Validate

Hi

 

<apex:page controller="Customer">
<script>
     function validate(){
     if (frm.pg.fname = ""
        alert("Name Cannot Be blank");
        return false;
        )
        else
        return true;
        }
</script>
    <apex:form id="frm" onsubmit="return validate()" >
        <apex:pageBlock id="pg" title="New Customer Entry">
            <p>First Name:
               <apex:inputText id="fname" value="{!firstName}"/></p>
            <p>Last Name:
               <apex:inputText value="{!lastName}"/></p>
            <p>Company Name:
               <apex:inputText value="{!companyName}"/></p>
            <p># Employees:
               <apex:inputText value="{!numEmployees}"/></p>
            <p>Department:
               <apex:inputText value="{!department}"/></p>
            <p>Email:
               <apex:inputText value="{!email}"/></p>
            <p>Phone:
               <apex:inputText value="{!phone}"/></p>
            <p>Title:
               <apex:inputText value="{!title}"/></p>
            <p>Address</p>
            <p>Street:
               <apex:inputText value="{!streetAddress}"/></p>
            <p>City:
               <apex:inputText value="{!cityAddress}"/></p>
            <p>State:
               <apex:inputText value="{!stateAddress}"/></p>
            <p>Zip:
               <apex:inputText value="{!postalCodeAddress}"/></p>
            <p>Country:
               <apex:inputText value="{!countryAddress}"/></p>

            <p><apex:commandButton action="{!save}"
               value="Save New Customer"/></p>
        </apex:pageBlock>
    </apex:form>
      <!-- Add related lists here -->
</apex:page

 

What is wrong in this form tag  "onsubmit="return validate()" "

When i don't write it is working fine , it adds record . I have put this condition to chech through Script that user should not enter blank First Name

 

Thanks

bob_buzzardbob_buzzard

This line:

 

 if (frm.pg.fname = ""

 is likely to give an error.  Visualforce will generate ids based on the identifiers that you supply, but they won't be identical.

 

You'll need to use the $Component global variable.  Something like:

 

var ele=document.getElementById('{!$Component.frm.pg.fname}');

 and then interrogate the element.

jagjitsingh@indivar.comjagjitsingh@indivar.com

Hi

 

  I have written this but still not working

 

<apex:page controller="Customer"> <script> function validate(){ var ele=document.getElementById('{!$Component.frm.pg.fname}'); if (ele = "" alert("Name Cannot Be blank"); return false; ) else return true; } </script> <apex:form id="frm" onsubmit="return validate()" > <apex:pageBlock id="pg" title="New Customer Entry"> <p>First Name: <apex:inputText id="fname" value="{!firstName}"/></p> <p>Last Name: <apex:inputText value="{!lastName}"/></p> <p>Company Name: <apex:inputText value="{!companyName}"/></p> <p># Employees: <apex:inputText value="{!numEmployees}"/></p> <p>Department: <apex:inputText value="{!department}"/></p> <p>Email: <apex:inputText value="{!email}"/></p> <p>Phone: <apex:inputText value="{!phone}"/></p> <p>Title: <apex:inputText value="{!title}"/></p> <p>Address</p> <p>Street: <apex:inputText value="{!streetAddress}"/></p> <p>City: <apex:inputText value="{!cityAddress}"/></p> <p>State: <apex:inputText value="{!stateAddress}"/></p> <p>Zip: <apex:inputText value="{!postalCodeAddress}"/></p> <p>Country: <apex:inputText value="{!countryAddress}"/></p> <p><apex:commandButton action="{!save}" value="Save New Customer"/></p> </apex:pageBlock> </apex:form> <!-- Add related lists here --> </apex:page>

Thanks

bob_buzzardbob_buzzard

Can you edit that last post so that the code is formatted?

jagjitsingh@indivar.comjagjitsingh@indivar.com

<apex:page controller="Customer">
<script>
     function validate()
{ var ele=document.getElementById('{!$Component.frm.pg.fname}');
 if (ele = "" alert("Name Cannot Be blank");
 return false; )
else
 return true;
        }
</script>
    <apex:form id="frm" onsubmit="return validate()" >
        <apex:pageBlock id="pg" title="New Customer Entry">
            <p>First Name:
               <apex:inputText id="fname" value="{!firstName}"/></p>
            <p>Last Name:
               <apex:inputText value="{!lastName}"/></p>
            <p>Company Name:
               <apex:inputText value="{!companyName}"/></p>
            <p># Employees:
               <apex:inputText value="{!numEmployees}"/></p>
            <p>Department:
               <apex:inputText value="{!department}"/></p>
            <p>Email:
               <apex:inputText value="{!email}"/></p>
            <p>Phone:
               <apex:inputText value="{!phone}"/></p>
            <p>Title:
               <apex:inputText value="{!title}"/></p>
            <p>Address</p>
            <p>Street:
               <apex:inputText value="{!streetAddress}"/></p>
            <p>City:
               <apex:inputText value="{!cityAddress}"/></p>
            <p>State:
               <apex:inputText value="{!stateAddress}"/></p>
            <p>Zip:
               <apex:inputText value="{!postalCodeAddress}"/></p>
            <p>Country:
               <apex:inputText value="{!countryAddress}"/></p>

            <p><apex:commandButton action="{!save}"
               value="Save New Customer"/></p>
        </apex:pageBlock>
    </apex:form>
      <!-- Add related lists here -->
</apex:page

bob_buzzardbob_buzzard

Your javascript is still a little mangled. I think you want something close to:

 

<script>
     function validate()
     { 
        var ele=document.getElementById('{!$Component.frm.pg.fname}');
        alert('Ele = ' + ele);
        if (ele.value == "")
        {
           alert("Name Cannot Be blank");
           return false;
        }
        else
           return true;
     }
</script>

 Note that I've added an alert to dump out the ele once its retrieved - if that is null or undefined it means the $Component ref isn't quite right.  if you are still having problems, add other alerts in to check the value of the element etc.

shra1_devshra1_dev

problem with your javascript function.

 

function validate(){

     var ele = document.getElementById('{!$Component.fname}').value;

 

     if(ele == ""){

     alert('Name cannot be blank');

     return false;

     }

     else

     return true;

}

 

 

Hope this solves your problem..

 

Regards,

Shravan