What you need to do is to trim the string before validation. Trimming a string involves removing whitespaces at the beginning and end of a string. So if a string only consists of a space then the trimmed version of that string will be empty.
Most languages have built-in functions for string trimming, e.g. string.Trim() in .NET, but JavaScript has no such function. You can however implement it with a little regexp like this:
var trimmed = str.replace(/^\s+|\s+$/g, '');
I also would like to recommend you to use the ClientId property of txtCCode instead of hard-coding the ctl00_ value that is prefixed automatically by the content placeholder.
So instead, you should write this as:
var value = document.getElementById('<% =txtCCode.ClientId %>').value.replace(/^\s+|\s+$/g, '');
if (window.value.length == 0)
{
...
}
If this post was useful to you, please mark it as answer. Thank you!
This approach only works if spaces are not allowed at all in the string. The trim approach will remove spaces around the string. Still if you write a sentence, spaces may be allowed within the string. But for passwords your method works nicely.
If this post was useful to you, please mark it as answer. Thank you!
you might also want to add a function which returns the clientid property of the textbox rather than referencing it explicitly, that should make your code less suceptible if/when you change your UI and the generated id changes...)
hi dudes.... blank check and single space validation can perform through the given code below functiion valid() { if(document.getElementById("").value=="") { alert("News Cannot Be Blank!!"); document.getElementById("").focus(); return false; } if(document.getElementById("").value=="
") { alert("News Cannot Be Blank!!"); document.getElementById("").focus(); return false; } return true; } but i want to know how we can avoid 2 or 3 spaces.. is there any validations for this... pls... reply quickly
balagangadha...
Member
79 Points
100 Posts
Empty value check for text box using javascript
May 30, 2008 08:35 AM|LINK
Hello All
Im developing web app using VS2005(C#).
i have a textbox control, i need to check it whether it have value or not using javascript
here is the script for it
if (window.document.getElementById(
'ctl00_ContentPlaceHolder1_txtCCode').value.length==0){
alert("Please Enter Value");
}
its working fine when the length in 0
but if i give a space in that textbox its not validating
how to do it
please help me
thanks in advance
Regards
Balagangadharan.R
suyog.dabhol...
Participant
857 Points
205 Posts
Re: Empty value check for text box using javascript
May 30, 2008 09:37 AM|LINK
Hi use this simple solution
it will surely work.
function CheckPassword(){
var txtPassword=document.getElementById('<%=TextBox2.ClientID %>'); if(txtPassword.value.indexOf(' ')>-1){
alert('No space');return false;}
else{
return true;}
}
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
<asp:Button ID="Button6" runat="server" Text="Enter Password" OnClientClick="return CheckPassword();" /></div>For more info follow this link
http://forums.asp.net/t/1267980.aspx
best luck
I am just using my free time,but if it helps you then my mark my post as answer
johram
All-Star
28531 Points
3567 Posts
Re: Empty value check for text box using javascript
May 30, 2008 09:38 AM|LINK
What you need to do is to trim the string before validation. Trimming a string involves removing whitespaces at the beginning and end of a string. So if a string only consists of a space then the trimmed version of that string will be empty.
Most languages have built-in functions for string trimming, e.g. string.Trim() in .NET, but JavaScript has no such function. You can however implement it with a little regexp like this:
I also would like to recommend you to use the ClientId property of txtCCode instead of hard-coding the ctl00_ value that is prefixed automatically by the content placeholder.
So instead, you should write this as:
var value = document.getElementById('<% =txtCCode.ClientId %>').value.replace(/^\s+|\s+$/g, ''); if (window.value.length == 0) { ... }johram
All-Star
28531 Points
3567 Posts
Re: Empty value check for text box using javascript
May 30, 2008 09:40 AM|LINK
This approach only works if spaces are not allowed at all in the string. The trim approach will remove spaces around the string. Still if you write a sentence, spaces may be allowed within the string. But for passwords your method works nicely.
balagangadha...
Member
79 Points
100 Posts
Re: Empty value check for text box using javascript
May 30, 2008 10:00 AM|LINK
Hello Johram
Thanks For replying
i tried ur code but its not validating whites spaces at beginning
please help me
thanks in advance
Regards
Balagangadharan.R
suyog.dabhol...
Participant
857 Points
205 Posts
Re: Empty value check for text box using javascript
May 30, 2008 10:13 AM|LINK
try this use full links
1)remove spaces present in textbox value
2)then check for length=0
http://www.mediacollege.com/internet/javascript/form/remove-spaces.html
http://www.faqts.com/knowledge_base/view.phtml/aid/1678/fid/1
http://www.nicknettleton.com/zine/javascript/trim-a-string-in-javascript
http://www.dreamincode.net/code/snippet1815.htm
http://www.webdeveloper.com/forum/archive/index.php/t-50427.html
I am just using my free time,but if it helps you then my mark my post as answer
dt_matthews
Member
18 Points
15 Posts
Re: Empty value check for text box using javascript
May 30, 2008 11:24 AM|LINK
you might also want to add a function which returns the clientid property of the textbox rather than referencing it explicitly, that should make your code less suceptible if/when you change your UI and the generated id changes...)
shijo28883
Member
2 Points
1 Post
Re: Empty value check for text box using javascript
Jul 04, 2008 06:25 AM|LINK