Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can you please help to solve this problem.
i have form
HTML
<div id ="regis" class="col-md-6 col-md-offset-1">
       <form >
        <p>
        Fisrt Name:
         <input id="fname1" class="input" type ="text"/>
        </p>
        <p>
          Last Name:
            <input id="lname1" class="input" type ="text"/>
        </p>
        <p>
            NIC Number:
            <input id="nic" class="input" type ="text"/>
        </p>
        <p>
            Email Address:
            <input id="eml" class="input" type ="text"/>
        </p>
           <p>
               Telephone Number:
               <input id=" tp" class="input" type ="text"/>
           </p>
           <button id="sub" class="btn-danger">Save </button>
    </form>
</div>


my jquerry file

JavaScript
$(document).ready(function (event){
    $("#sub").click(function(){  //submit btn
       var  ffname = $("#fname1"); //fisrst name
       var  lname = $("#lname1");
       var  nic = $("#nic");
       var  eml1 = $("#eml");
       var  tp = $("#tp");


        if(ffname.val() ==""){
            $("this").css("background-color" , "red");
        }
        else
        {
            $("this").css("background-color" , "green");
        }

    });



});


but unfortunately this is not validating
Posted
Comments
Sergey Alexandrovich Kryukov 10-Apr-15 11:41am    
Perhaps jQuery does not have being disappointed with incorrect spelling of its name. :-)
—SA
nuke_infer 10-Apr-15 11:46am    
SA - please need solution

1 solution

What is $("this")? Nothing. It should be $(this) which is a reference to the element that invokes the current function, that is the button. But if you mean to change the color of the invalid element say the ffname textbox, then it should be
ffname.css("background-color" , "red");

See demo[^]
++++++[Last Round]+++++
You code is a mess, did you write it yourself? Do you understand the code? If not, you should consider some tutorials to equip yourself with the necessary knowledge, else no amount of explanation can help you.Start from the basic and learn one thing at a time. At a last bit, I have put together a small example. The rest is up to you to figure out.
XML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#sub").click(function(){  //submit btn
       var  ffname = $("#fname1"); //fisrst name

        if(ffname.val() ==""){
            ffname.css("background-color" , "red");
        }
        else
        {
            ffname.css("background-color" , "green");
        }

        // to prevent default action and propagation.
        return false;

    });
});
</script>
</head>
<body>
<form method="post">
        <p>
        Fisrt Name:
         <input id="fname1" class="input" type ="text"/>
        </p>
           <button id="sub" class="btn-danger">Save </button>
</form>
</div>

</body>
</html>

Good luck.
 
Share this answer
 
v6
Comments
nuke_infer 10-Apr-15 11:35am    
yes i have changed it $(this) and changed in as ffname also i used these two method but same issues it is not validating
Peter Leow 10-Apr-15 11:46am    
What issue? Be more specific, we cannot see your screen. The background color of ffname should have changed, right? Have you included the jQuery library?
Sergey Alexandrovich Kryukov 10-Apr-15 11:47am    
Ha-ha, good catch, a 5.
—SA
Peter Leow 10-Apr-15 11:55am    
Thank you, Sergey.
nuke_infer 10-Apr-15 11:48am    
if i click the button without inserting the values it is not become red

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900