By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,222 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,222 IT Pros & Developers. It's quick & easy.

Javascript validation for spaces in text box

P: 18
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).
Mar 24 '08 #1
Share this Question
Share on Google+
21 Replies


kenobewan
Expert 2.5K+
P: 4,871
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.
Mar 24 '08 #2

P: 18
please send me javascript code for that(Space validation) it is very importent in my project...
Mar 24 '08 #3

P: 18
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...
Mar 25 '08 #4

P: 18
please reply me for the abobe question...
Mar 25 '08 #5

malav123
100+
P: 217
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...
Mar 25 '08 #6

P: 18
how to use ascii value or trim for that text box
Mar 25 '08 #7

malav123
100+
P: 217
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....
Mar 25 '08 #8

P: 18
validation in which spaces not allows in textbox
Mar 25 '08 #9

P: 18
please reply me for the above question...
Mar 25 '08 #10

gits
Expert Mod 5K+
P: 5,390
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. }
  9.  
kind regards
Mar 25 '08 #11

P: 18
Thank you for sending the code.


kiran
Mar 25 '08 #12

gits
Expert Mod 5K+
P: 5,390
no problem ... does it work for you?

kind regards
Mar 25 '08 #13

malav123
100+
P: 217
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;
}
Mar 25 '08 #14

gits
Expert Mod 5K+
P: 5,390
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+$/, '');
  2.  
it could be just one line ;)

kind regards
Mar 25 '08 #15

malav123
100+
P: 217
Ya it's true....
this will benificial for me also..
thanks....
Mar 25 '08 #16

gits
Expert Mod 5K+
P: 5,390
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
Mar 25 '08 #17

malav123
100+
P: 217
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....
Mar 27 '08 #18

gits
Expert Mod 5K+
P: 5,390
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
Mar 27 '08 #19

acoder
Expert Mod 15k+
P: 16,027
This is obviously a JavaScript problem thread. Moved to the JavaScript forum. Please post in the relevant forum in future.

Moderator.
Mar 27 '08 #20

malav123
100+
P: 217
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...
Mar 28 '08 #21

acoder
Expert Mod 15k+
P: 16,027
It was addressed to the OP (kiran83), but if you benefited, you're welcome.
Mar 31 '08 #22

Post your reply

Sign in to post your reply or Sign up for a free account.