Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Javascript validation for spaces in text box

Question posted by: kiran83 (Newbie) on March 24th, 2008 11:44 AM
My Text box can accept all types of data like alphabets,numeric,special characters ... Should give an error message 'if space is not a valid value '
when i am clicking a button... in
asp.net with c# through javascript.(javascript validation after clicking a button).
kenobewan's Avatar
kenobewan
Moderator
4,538 Posts
March 24th, 2008
11:46 AM
#2

Re: Javascript validation for spaces in text box
Quote:
My Text box can accept all types of data like alphabets,numeric,special characters ... Should give an error message 'if space is not a valid value '
when i am clicking a button... in
asp.net with c# through javascript.(javascript validation after clicking a button).

This is still a JS problem. Suggest using click event and regular expressions. HTH.

Reply
kiran83's Avatar
kiran83
Newbie
18 Posts
March 24th, 2008
11:54 AM
#3

Re: Javascript validation for spaces in text box
please send me javascript code for that(Space validation) it is very importent in my project...

Reply
kiran83's Avatar
kiran83
Newbie
18 Posts
March 25th, 2008
05:19 AM
#4

Re: Javascript validation for spaces in text box
i was tryed the below code


function validate()
{
if (document.getElementById("<%=TextBox1.ClientID%>").value=="")
{
alert("No Feild can not be blank");
document.getElementById("<%=TextBox1.ClientID%>").focus();
return false;
}
}

this code only validate without blank but i want to without spaces validation

please help me...

Reply
kiran83's Avatar
kiran83
Newbie
18 Posts
March 25th, 2008
06:17 AM
#5

Re: Javascript validation for spaces in text box
please reply me for the abobe question...

Reply
malav123's Avatar
malav123
Familiar Sight
211 Posts
March 25th, 2008
06:24 AM
#6

Re: Javascript validation for spaces in text box
Quote:
please reply me for the abobe question...

Hi Kiran,

You can use Ascii value for validation... or you can use trim function to remove all spaces from your text boxes...

Reply
kiran83's Avatar
kiran83
Newbie
18 Posts
March 25th, 2008
06:51 AM
#7

Re: Javascript validation for spaces in text box
how to use ascii value or trim for that text box

Reply
malav123's Avatar
malav123
Familiar Sight
211 Posts
March 25th, 2008
07:03 AM
#8

Re: Javascript validation for spaces in text box
Quote:
how to use ascii value or trim for that text box


HI,
Fisrt of all tell me what you needs exactly ? do you want to put validation in which spaces not allows in textbox or you want to cheks only blank textbox validation ?? I think you don't want users to put spaces in text box.. right ? tell exact requirement than i will tell you how to use it because i have already used such validation in my project....

Reply
kiran83's Avatar
kiran83
Newbie
18 Posts
March 25th, 2008
07:18 AM
#9

Re: Javascript validation for spaces in text box
validation in which spaces not allows in textbox

Reply
kiran83's Avatar
kiran83
Newbie
18 Posts
March 25th, 2008
07:51 AM
#10

Re: Javascript validation for spaces in text box
please reply me for the above question...

Reply
gits's Avatar
gits
Moderator
3,202 Posts
March 25th, 2008
09:26 AM
#11

Re: Javascript validation for spaces in text box
have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. function validate() {
  2.     var field = document.getElementById("<%=TextBox1.ClientID%>");
  3.     var val   = field.value.replace(/^\s+|\s+$/, '');
  4.  
  5.     if (val.length == 0) {
  6.         alert('wrong value');
  7.     }
  8. }

kind regards

Reply
kiran83's Avatar
kiran83
Newbie
18 Posts
March 25th, 2008
10:35 AM
#12

Re: Javascript validation for spaces in text box
Thank you for sending the code.


kiran

Reply
gits's Avatar
gits
Moderator
3,202 Posts
March 25th, 2008
10:38 AM
#13

Re: Javascript validation for spaces in text box
no problem ... does it work for you?

kind regards

Reply
malav123's Avatar
malav123
Familiar Sight
211 Posts
March 25th, 2008
12:58 PM
#14

Re: Javascript validation for spaces in text box
If you want to validate the textboxes for space than you can use followin java script function,

function trimAll(sString)
{
while (sString.substring(0,1) == ' ')
{
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ' ')
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}

Reply
gits's Avatar
gits
Moderator
3,202 Posts
March 25th, 2008
01:06 PM
#15

Re: Javascript validation for spaces in text box
yes ... that is another way to trim a value that is done with the replace-method and a regex in the above code:

Expand|Select|Wrap|Line Numbers
  1. var val = field.value.replace(/^\s+|\s+$/, '');

it could be just one line ;)

kind regards

Reply
malav123's Avatar
malav123
Familiar Sight
211 Posts
March 25th, 2008
01:12 PM
#16

Re: Javascript validation for spaces in text box
Ya it's true....
this will benificial for me also..
thanks....

Reply
gits's Avatar
gits
Moderator
3,202 Posts
March 25th, 2008
01:37 PM
#17

Re: Javascript validation for spaces in text box
Quote:
Ya it's true....
this will benificial for me also..
thanks....

no problem :) ... glad when it helps you ... and a note: whenever string-operations have to be done in javascript we could try to use regex for that. the downside of it is just the 'readability' ... sometimes regex looks very 'cryptic' and hard to read ...

kind regards

Reply
malav123's Avatar
malav123
Familiar Sight
211 Posts
March 27th, 2008
02:27 PM
#18

Re: Javascript validation for spaces in text box
Quote:
no problem :) ... glad when it helps you ... and a note: whenever string-operations have to be done in javascript we could try to use regex for that. the downside of it is just the 'readability' ... sometimes regex looks very 'cryptic' and hard to read ...

kind regards


Hi,
i am using one check box in my project and when i clicks on check box one pannel opens, i have write the code for this on server side but i want to write the code of same in javascript... i mean on client side, so please give me solution for my problem....

Reply
gits's Avatar
gits
Moderator
3,202 Posts
March 27th, 2008
05:01 PM
#19

Re: Javascript validation for spaces in text box
Quote:
Hi,
i am using one check box in my project and when i clicks on check box one pannel opens, i have write the code for this on server side but i want to write the code of same in javascript... i mean on client side, so please give me solution for my problem....

since this is a very different question and doesn't belong to this thread i would recommend you to start a new thread regarding your NEW problem in the javascript forum ...

kind regards
MOD

Reply
acoder's Avatar
acoder
Site Moderator
11,540 Posts
March 27th, 2008
05:39 PM
#20

Re: Javascript validation for spaces in text box
This is obviously a JavaScript problem thread. Moved to the JavaScript forum. Please post in the relevant forum in future.

Moderator.

Reply
malav123's Avatar
malav123
Familiar Sight
211 Posts
March 28th, 2008
06:46 PM
#21

Re: Javascript validation for spaces in text box
Quote:
This is obviously a JavaScript problem thread. Moved to the JavaScript forum. Please post in the relevant forum in future.

Moderator.



Thanks for Suggestion...
I am using your site from several days but i was using only .net section of Forums today i opens this section... so i will follow as you suggested from today...

Reply
acoder's Avatar
acoder
Site Moderator
11,540 Posts
March 31st, 2008
01:38 PM
#22

Re: Javascript validation for spaces in text box
It was addressed to the OP (kiran83), but if you benefited, you're welcome.

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,284 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Javascript / DHTML / Ajax Forum Contributors