Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I am new with javascript please help me to solve this small problem..

how to make textbox required on buttonclick through javascript.
Posted

HTML
<!DOCTYPE html>
<html>
<head>
<script>
function validateNameInput()
{
	var value = document.getElementById('nameInput').value;
	if (value.length < 4)
		alert('String is too short. Must be 4 or more chars long\n\n(you used ' + value.length + ')');
	else
		alert('String valid\n\nLength: '+value.length);
}
</script>
</head>
<body>
	<label for='nameInput'>Enter a string - must be longer than 3 chars</label>
	<input id='nameInput' />
	<button onclick='validateNameInput();'>Validate</button>
</body>
</html>
 
Share this answer
 
Comments
pasztorpisti 27-Aug-12 17:29pm    
+5 pure and simple
enhzflep 28-Aug-12 17:07pm    
Thanks mate. :)
Here is a sample code of how to make a text box required field.

XML
<asp:TextBox ID="Textbox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return ValidateTextBox()" />


XML
<script type="text/javascript">
   function ValidateTextBox()
   {
       var Text = document.getElementById('<%=Textbox1.ClientID %>');

       if(Text.value == "")
       {
           alert('No data entered in textbox');
           return false;
       }

   }
   </script>



Since you are new to JavaScript you might find the below links useful
JavaScript Tutorial[^]

Beginner JavaScript[^]

Jquery Tutorial[^]
 
Share this answer
 
Hi,

When anything i need to work with javascript, i use jQuery(personally i like it:))

So for your solution i would like to suggest you to use jQuery.

Please have a look at below link,
RequiredFieldValidation using jQuery[^]

Hope you enjoy working with jQuery.

Thanks
-Amit Gajjar
 
Share this answer
 
hi, try it like this:

JavaScript
<input name="crm_surname" id="crm_surname" type="text" size="20" maxlength="200" style="width: 200px;" /><label>*</label>
                    <label id="nameRequired" style="display: none;">
                        name is required</label>

<button onclick='checkNameRequired();'>button</button>


C#
function checkNameRequired() {
           var txtName = document.getElementById("crm_surname");
           var nameRequired = document.getElementById("nameRequired");
           if (txtName == "") {
               nameRequired.style.display = "";
                return false;
           }
           else {
               nameRequired.style.display = "none";
           }
           return true;
       }
 
Share this answer
 
v2
Take a look at this links, all come with code samples and explanations:

http://sbpoley.home.xs4all.nl/webmatters/formval.html[^]

http://rickharrison.github.com/validate.js/[^]

http://www.w3schools.com/js/js_form_validation.asp[^]

Keep in mind that there are people out there with javascript disabled so, checking things with javascript is correct, but making that again using server side validation (PHP...) is a good idea. You should get problems in the sever side validation if you've passed the JS one.
With JS you get easiness and you don't need to post the form in order to check it, but consider that.

Good luck!
 
Share this answer
 
v2

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