Last post Jul 04, 2008 07:25 AM by shijo28883
May 30, 2008 09:35 AM|balagangadharanr|LINK
Im developing web app using VS2005(C#).
here is the script for it
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
May 30, 2008 10:37 AM|suyog.dabhole|LINK
Hi use this simple solution
it will surely work.
For more info follow this link
May 30, 2008 10:38 AM|johram|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.
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)
May 30, 2008 10:40 AM|johram|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.
May 30, 2008 11:00 AM|balagangadharanr|LINK
Thanks For replying
i tried ur code but its not validating whites spaces at beginning
May 30, 2008 11:13 AM|suyog.dabhole|LINK
try this use full links
1)remove spaces present in textbox value
2)then check for length=0
May 30, 2008 12:24 PM|dt_matthews|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...)
Jul 04, 2008 07:25 AM|shijo28883|LINK