Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to check for blanks in textbox using javascript

Status
Not open for further replies.

duckyboyz

Programmer
Mar 28, 2002
57
US
i'm using the following code to try to check for blanks in textbox1 (lastname):

<%@ Page Language="VB" %>
<script runat="server">

' Insert page code here
'

Sub Button1_Click(sender As Object, e As EventArgs)
response.redirect("homeApp.aspx")
End Sub

</script>
<html>
<head>
<script language="JavaScript">

function validatePage(sender, args)
{
if (document.getElementById("textbox1").value.length < 2)
{
args.IsValid = false;
return false;
}
else
{
args.IsValid = true;
return true;
}

}

</script>
</head>
<body>
<form id="form1" runat="server">
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage="CustomValidator" ControlToValidate="TextBox1" ClientValidationFunction="validatePage"></asp:CustomValidator>
</p>
<p>
</p>
<p>
<asp:Label id="Label2" runat="server">something 2</asp:Label>&nbsp;&nbsp;
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
</p>
<p>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="home"></asp:Button>
</p>
<p>
<!-- Insert content here -->
</p>
</form>
</body>
</html>
i can use "document.getElementById("textbox1").value.length < 2)" to check for atleast one char in the textbox, but < 0 does'nt work for no chars in the textbox1 field.

how do you check for blanks?

Mike Johnson
 
Why don't you use a RequiredFieldValidator?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
If you are searching for blanks within a textbox then a customvalidator is what you need.

If you are trying to ensure that something has been entered then the RequiredFieldValidator is what you want.
 
Microsoft said:
Extra spaces at the beginning and end of the input value are removed before validation is performed. This prevents a space being entered in the input control from passing validation.
The RequiredFieldValidator already does this so you don't need to use a CustomValidator.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I do know that, I was trying to explain that if he were looking for blanks (spaces) within a textbox then a customvalidator is what he would need.

Maybe a field with login that shouldn't have spaces, like:

"joewilliams" not "joe williams"

I over analyzed the question.
 
no... it was requested that all validation be client-side, i.e. javascript.

Mike Johnson
 
i use this RegExp
Code:
function checktext(thefieldname,s) {
  var thefield = document.getElementById(thefieldname);
  var istxt = thefield.value;
  istxt = istxt.replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s+/g," ");
  if(istxt != '') {
    return true;
  } else {
    if (s != '') { 
      alert(s);
    }
    thefield.focus();
    return false;
  } 
}

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
"no... it was requested that all validation be client-side, i.e. javascript."

The RequiredFieldValidator does perform validation on the client-side using javascript. It only works for IE in 1.x but I think they fixed the scripts in 2.0 for cross-browser compatibility.

If you try to do the validation your own way, you'll miss out on a lot of other snazzy validation framework features: it isn't recommended.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top