+ Start a Discussion
prasanth puvvada 4prasanth puvvada 4 

javascript textbox not validating if empty

senario:- in the textbox if i dont enter any data and click on submit then it say please enter name.  

This page is not showing any error. after executing the desired output is not coming.

<apex:page id="page">
<script>
function show()
{
var name=document.GetElementById('page:f1:p1:ip1').value;
if(name="" || name=nulll)
{
document.GetElementBy.id('page:f1:p1:op2').innerHtml='please enter your name';
}

</script>

<apex:form id="f1">
<apex:pageblock id="p1">
<apex:outputlabel value="Enter u r name" id="op1" />
<apex:inputtext id="ip1" />
<apex:commandbutton value="submit" onclick="show();" />
<apex:outputlabel id="op2" />




</apex:pageblock>
</apex:form>

</apex:page>
Best Answer chosen by prasanth puvvada 4
Arunkumar RArunkumar R
Hi Prasanth,

NOTE: JavaScript is Case Sensitive. So be aware before starting javascript code. 

I have modified your code, take a look on the below code. If the below solutions works for you, then please mark this as solution. :)
 
<apex:page id="page">
    
    <script>
    function show()
    {
        var name=document.getElementById('page:f1:p1:ip1').value;
        if(name== "" || name==null)
        {
            document.getElementById("page:f1:p1:op2").innerHTML = "Please enter your name"; 
        }
    }
    </script>
    
    <apex:form id="f1">
        <apex:pageblock id="p1">
            <apex:outputlabel value="Enter u r name" id="op1" />
            <apex:inputtext id="ip1" />
            <apex:commandbutton value="submit" onclick="return show();" reRender="op2"/>
            <apex:outputlabel id="op2" />
        </apex:pageblock>
    </apex:form>
    
</apex:page>

You can start your javascript basic at http://www.w3schools.com/js/

All Answers

prasanth puvvada 4prasanth puvvada 4
i have corrected some errors even though the output is nt coming. please help 
<apex:page id="page">
<script>
function show()
{
var name=document.getElementById('page:f1:p1:ip1').value;
if(name=='' || name==null)
{
document.getElementById('page:f1:p1:op2').innerHtml='please enter your name';

}
}

</script>

<apex:form id="f1">
<apex:pageblock id="p1">
<apex:outputlabel value="Enter u r name" id="op1" />
<apex:inputtext id="ip1" />
<apex:commandbutton value="submit" onclick="show();" />
<apex:outputlabel id="op2" />




</apex:pageblock>
</apex:form>

</apex:page>

 
Arunkumar RArunkumar R
Hi Prasanth,

NOTE: JavaScript is Case Sensitive. So be aware before starting javascript code. 

I have modified your code, take a look on the below code. If the below solutions works for you, then please mark this as solution. :)
 
<apex:page id="page">
    
    <script>
    function show()
    {
        var name=document.getElementById('page:f1:p1:ip1').value;
        if(name== "" || name==null)
        {
            document.getElementById("page:f1:p1:op2").innerHTML = "Please enter your name"; 
        }
    }
    </script>
    
    <apex:form id="f1">
        <apex:pageblock id="p1">
            <apex:outputlabel value="Enter u r name" id="op1" />
            <apex:inputtext id="ip1" />
            <apex:commandbutton value="submit" onclick="return show();" reRender="op2"/>
            <apex:outputlabel id="op2" />
        </apex:pageblock>
    </apex:form>
    
</apex:page>

You can start your javascript basic at http://www.w3schools.com/js/
This was selected as the best answer
Sunil PalSunil Pal
HI Prasanth,

I have jquery. Please use the jquery file in static resource it will help later on also.

<apex:page id="page">
<script>
function show()
{

var name = $('.inputText').val();

if(name === "" || name === null)
{
    $('.errorMsg').show();
}
else {
    $('.errorMsg').hide();
}

</script>
<style> 
    .errorMsg {
    
        display: none;
    }
</style>

<apex:form id="f1">
<apex:pageblock id="p1">
<apex:outputlabel value="Enter u r name" id="op1" />
<apex:inputtext id="ip1" styleClass="inputText"/>
<apex:commandbutton value="submit" onclick="show();" />
<apex:outputlabel id="op2" styleClass="errorMsg" >
    Please eneter your name
</apex:outputlabel>

</apex:pageblock>
</apex:form>

</apex:page>

Please let me know if it is helpfull..


Thanks
Sunil
prasanth puvvada 4prasanth puvvada 4
dear sunil, thanks for your new code. i will try it. Thankyou