通过javascript的requiredField验证器
问题描述:
大家好,我是javascript新手,请帮助我解决这个小问题.
Hi all, I am new with javascript please help me to solve this small problem..
how to make textbox required on buttonclick through javascript.
答
<!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>
此处是如何制作文本框必填字段的示例代码.
Here is a sample code of how to make a text box required field.
<asp:TextBox ID="Textbox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return ValidateTextBox()" />
<script type="text/javascript">
function ValidateTextBox()
{
var Text = document.getElementById('<%=Textbox1.ClientID %>');
if(Text.value == "")
{
alert('No data entered in textbox');
return false;
}
}
</script>
由于您不熟悉JavaScript,因此您可能会发现以下链接有用
JavaScript教程 [ ^ ]
初学者JavaScript [ ^ ]
Jquery教程 [ ^ ]
Since you are new to JavaScript you might find the below links useful
JavaScript Tutorial[^]
Beginner JavaScript[^]
Jquery Tutorial[^]
当我需要使用javascript进行任何操作时,我都使用jQuery(个人喜欢:))
因此,对于您的解决方案,我建议您使用jQuery.
请看下面的链接,
使用jQuery的RequiredFieldValidation [
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