+ Start a Discussion
force shahidforce shahid 

Why error msg is not showing ?

Hi Friends,

I write a program on javascript base. When i give the less than 3 letters user name it wil display error msg. But its not working and also F1 is not woking. Please check this code and help me to solve this problem,

Code:


<apex:page >
    <script>
    function f1(uid,pwd){
        var u=document.getElementByid(uid).value;
        var p=document.getElementByid(pwd).value;
        if(u==""){
            alert(' Please enter User name');
            document.getelementbyid(uid).focus();
        }
        else{
            if(u.length<3)
                alert('Please Enter username minimum 3 Characters');
        }
        if(p==""){
            alert('Please Enter Password');
            document.getelementbyid(pwd).focus();
        }
    }
    function f2(uid,pwd){
        document.getelementbyid(uid).value="";
        document.getelementbyid(pwd).value="";
    }
    </script>
    
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection title=" Entry Section">
                <apex:outputText value="Enter User Name"></apex:outputText>
                <apex:inputText id="uid"/>
                <apex:outputText value="Enter Password"></apex:outputText>
                <apex:inputText id="pwd"/>
                
                <apex:commandButton value="Submit" onclick="f1('{!$component.uid}','{!$component.pwd}')"/>
                <apex:commandButton value="CLear" onclick="f2('{!$component.uid}','{!$component.pwd}'"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Best Answer chosen by force shahid
Dayakar.DDayakar.D
Hi Shahid,
The problem here is JavaScript is a case-sensitive language. This means that the language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

User-added image
we should use statement like below 
document.getElementById(uid).value;

Please find below code it will work.
<apex:page >
    <script>
    function f1(uid,pwd){
        var u=document.getElementById(uid).value;
        var p=document.getElementById(pwd).value;
        if(u==""){
            alert(' Please enter User name');
            document.getElementById(uid).focus();
        }
        else{
            if(u.length<3)
                alert('Please Enter username minimum 3 Characters');
        }
        if(p==""){
            alert('Please Enter Password');
            document.getelementbyid(pwd).focus();
        }
    }
    function f2(uid,pwd){
        document.getElementById(uid).value= "";
        document.getElementById(pwd).value= "";
        
    }
    </script>
    
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection title=" Entry Section">
                <apex:outputText value="Enter User Name"></apex:outputText>
                <apex:inputText id="uid"/>
                <apex:outputText value="Enter Password"></apex:outputText>
                <apex:inputText id="pwd"/>
                
                <apex:commandButton value="Submit" onclick="f1('{!$component.uid}','{!$component.pwd}')"/>
                <apex:commandButton value="CLear" onclick="f2('{!$component.uid}','{!$component.pwd}')"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please let me know, if it helps you.

Best Regards,
Dayakar.D

 

All Answers

Dayakar.DDayakar.D
Hi Shahid,
The problem here is JavaScript is a case-sensitive language. This means that the language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

User-added image
we should use statement like below 
document.getElementById(uid).value;

Please find below code it will work.
<apex:page >
    <script>
    function f1(uid,pwd){
        var u=document.getElementById(uid).value;
        var p=document.getElementById(pwd).value;
        if(u==""){
            alert(' Please enter User name');
            document.getElementById(uid).focus();
        }
        else{
            if(u.length<3)
                alert('Please Enter username minimum 3 Characters');
        }
        if(p==""){
            alert('Please Enter Password');
            document.getelementbyid(pwd).focus();
        }
    }
    function f2(uid,pwd){
        document.getElementById(uid).value= "";
        document.getElementById(pwd).value= "";
        
    }
    </script>
    
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection title=" Entry Section">
                <apex:outputText value="Enter User Name"></apex:outputText>
                <apex:inputText id="uid"/>
                <apex:outputText value="Enter Password"></apex:outputText>
                <apex:inputText id="pwd"/>
                
                <apex:commandButton value="Submit" onclick="f1('{!$component.uid}','{!$component.pwd}')"/>
                <apex:commandButton value="CLear" onclick="f2('{!$component.uid}','{!$component.pwd}')"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please let me know, if it helps you.

Best Regards,
Dayakar.D

 
This was selected as the best answer
force shahidforce shahid
Thanks Dayakar, Now its working.