Using Regular Expression Validator for removing consecutive white spaces with single white space

Hi

I had a textbox feild in which I want to remove consecutive white spaces with single white spaces from beginning,end and middle of text

and I want to use Regular Expression Validator to do this

Looking out for some Help

Kartik

0
vki
3/20/2008 4:59:47 AM
asp.net.web-forms 93655 articles. 5 followers. Follow

15 Replies
1181 Views

Similar Articles

[PageSpeed] 33

Hi,
Regular Expression Validator will not remove the space.
It will throw a client side validation.
To remove space use Trim method on server side.


Virendra (MCTS)

My SiteBlog
0
virendra1983
3/20/2008 5:14:27 AM

Hi!

If you want to do it on server side then following is the vb.net code:

Imports System.Text.RegularExpressions

-----------------------------------------

Dim r As Regex = New Regex("\s")Dim a As String = r.Replace("ne p st e r", "")

You can replace "ne p st e r" with your string.

Same will work on client side using java script:

Regex r = new Regex(@"\s");
dest = r.Replace(str, "");

Hope this will help!

Nepster from India Yes

Mark this post as answer if it help you


Hope this will help!

Nepster from India Yes
Note: Please mark this post as answer if it help you.
0
nepster_from_india
3/20/2008 5:17:27 AM

Hi

Thanks for ur concern

I am using the folowing code (Code is in C#)

Regex r = new Regex("\s");

TextBox1.Text=r.Replace(TextBox1.Text);

But it is givng an error saying:Unrecognized Escape sequence Regex r = new Regex("\s");

Any Idea about this 

0
vki
3/24/2008 4:30:07 AM

Hi,
Change your code to this...

Regex r = new Regex(@"\s");


Virendra (MCTS)

My SiteBlog
0
virendra1983
3/24/2008 4:44:55 AM

try

Regex r = new Regex("[\s]");


Hari Krishnan
techiebuddy.com
0
hari_4uin
3/24/2008 4:47:29 AM

I got the expression for trimming space

Problem I am facing now is textbox is in Gridview and I want to trim spaces in that textbox and that has to get updated in database

which is not happening

I had used the folllowing code in row updating event of gridview as I want update the databse through textbox value

int a = gvaccountmapping.EditIndex;

GridViewRow row = gvaccountmapping.Rows[a];

TextBox textaccount = new TextBox();

textaccount = (TextBox)row.FindControl("editProductName");

string pattern = @"/^\s*|\s(?=\s)|\s*$/g";

textaccount.Text = Regex.Replace(textaccount.Text, pattern, "");

 

Here the value with trimmed spaces is not going to database instead of this value with spaces is going to database

Any idea about this

 

0
vki
3/24/2008 6:38:03 AM

Hi,

I got the your problem..
The problem is with the Regular Expression.. It is not counting the space..
Change your string pattern to this

string pattern = @"\s";
It is working fine..


Virendra (MCTS)

My SiteBlog
0
virendra1983
3/24/2008 6:50:34 AM

I got the expression for trimming space

Problem I am facing now is textbox is in Gridview and I want to trim spaces in that textbox and that has to get updated in database

which is not happening

I had used the folllowing code in row updating event of gridview as I want update the databse through textbox value

int a = gvaccountmapping.EditIndex;

GridViewRow row = gvaccountmapping.Rows[a];

TextBox textaccount = new TextBox();

textaccount = (TextBox)row.FindControl("editProductName");

string pattern = @"/^\s*|\s(?=\s)|\s*$/g";

textaccount.Text = Regex.Replace(textaccount.Text, pattern, "");

 

Here the value with trimmed spaces is not going to database instead of this value with spaces is going to database

Any idea about this

0
vki
3/24/2008 9:39:44 AM

Hi, You have already post the problem in your previous post of this thread.
I replied to it...

virendra1983:

Hi,

I got the your problem..
The problem is with the Regular Expression.. It is not counting the space..
Change your string pattern to this

string pattern = @"\s";
It is working fine..



Please change your Regular Expression.
Virendra (MCTS)

My SiteBlog
0
virendra1983
3/24/2008 9:49:55 AM

vki:

I got the expression for trimming space

Problem I am facing now is textbox is in Gridview and I want to trim spaces in that textbox and that has to get updated in database

which is not happening

I had used the folllowing code in row updating event of gridview as I want update the databse through textbox value

int a = gvaccountmapping.EditIndex;

GridViewRow row = gvaccountmapping.Rows[a];

TextBox textaccount = new TextBox();

textaccount = (TextBox)row.FindControl("editProductName");

string pattern = @"/^\s*|\s(?=\s)|\s*$/g";

textaccount.Text = Regex.Replace(textaccount.Text, pattern, "");

 

Here the value with trimmed spaces is not going to database instead of this value with spaces is going to database

Any idea about this

 

Hi vki,

Can I see the whole code?

Waiting for your feedback,

Hong Gang 


Hong-Gang Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Hong
3/24/2008 10:03:51 AM

Thanks for ur reply

but regular expression that I used fits into my requirements

I am facing some more problems which I posted 

Thanks

0
vki
3/26/2008 5:27:15 AM

This is the code I used in Row Updateing event of Gridview 

In this I am setting up the connection trimming the white spaces in textbox value

then passing the stored procedure name with parameters to update the database

I removed UpdateCommand and UpdateCommandType from my sql datasource in front end

Now it is giving error: "Updatecommand is not specified"

I don't able to understand why it is not reading the stored procedure name

specified in com.CommandText

/////////////////////////////////////////////Code/////////////////////////////////////////////////////////////////////////

int a = gvaccountmapping.EditIndex;

int b = e.RowIndex;

object id = gvaccountmapping.DataKeys[b].Value;

GridViewRow row = gvaccountmapping.Rows[a];

TextBox textaccount = new TextBox();

textaccount = (TextBox)row.FindControl("editProductName");

TextBox textaccount1 = new TextBox();

textaccount1 = (TextBox)row.FindControl("editAccountdescName");

string pattern = @"/^\s*|\s(?=\s)|\s*$/g";

textaccount.Text = Regex.Replace(textaccount.Text, pattern,"");

((TextBox)row.FindControl("editProductName")).Text = textaccount.Text;

string STR = ((TextBox)row.FindControl("editProductName")).Text;

// string ConnctionString = ConfigurationManager.ConnectionStrings["ConSql"].ToString();

SqlConnection con = new SqlConnection("Data Source=dotnetsvrs;uid=saa;pwd=saa;database=BMS");SqlCommand com = new SqlCommand();

com.Connection = con;

con.Open();

com.CommandText =
"update_financial_account_mapping";

com.CommandType =CommandType.StoredProcedure;

com.Parameters.AddWithValue("@Account_Id", id.ToString());

com.Parameters.AddWithValue("@Financial_Account_Number", textaccount.Text);

com.Parameters.AddWithValue("@Financial_Account_Desc", textaccount1.Text);com.Parameters.AddWithValue("@Last_Updated_By", Session["Admin"].ToString());

com.ExecuteNonQuery();

con.Close();

 /////////////////////////////////////////////Code/////////////////////////////////////////////////////////////////////////

Please Help me out in This

0
vki
3/26/2008 5:39:31 AM
Hi,

vki:

textaccount.Text = Regex.Replace(textaccount.Text, pattern, "");



Although you assigned it a new value but May be your text box is loosing it's value.
Do one thing..

string strAccount = Regex.Replace(textaccount.Text, pattern, "");
Pass strAccount in the Database instead of textaccount.Text.


Virendra (MCTS)

My SiteBlog
0
virendra1983
3/26/2008 5:43:35 AM

Try this... 

Regex reg=new Regex(@"/^\s*|\s(?=\s)|\s*$/g");

com.Parameters.AddWithValue("@Financial_Account_Number", reg.replace(textaccount.Text,""));

 


Hari Krishnan
techiebuddy.com
0
hari_4uin
3/27/2008 2:18:54 AM

Thanks for ur Reply

But still it is giving the following error

"Updating is not supported by data source 'accountmappingdatatsource' unless UpdateCommand is specified"

Here interesting thing is that now value is getting trimmed and it is also updated in database but the gridview is in Update mode only

Mode of gridview is not changing to normal

0
vki
3/27/2008 3:57:56 AM
Reply:

Similar Artilces:

Preserving white space in iframe control using style sheet="white-space:pre"
I am suing iframe control in the edit mode. I need to preserve white space in the control. e.g. if I put following data in the control and then save in the database, SSDB.CLMP1SO              735837              735837               0           0SSDB.CNCP2SO              735837              735837               0           0SSDB.CYMP...

superreview granted: [Bug 312747] Include line break info for text using white-space: pre : [Attachment 199824] If text has computed style of white-space: pre, then don't expose
Boris Zbarsky <bzbarsky@mit.edu> has granted Aaron Leventhal (vacation: mostly gone until Oct 17) <aaronleventhal@moonset.net>'s request for superreview: Bug 312747: Include line break info for text using white-space: pre https://bugzilla.mozilla.org/show_bug.cgi?id=312747 Attachment 199824: If text has computed style of white-space: pre, then don't expose https://bugzilla.mozilla.org/attachment.cgi?id=199824&action=edit ...

superreview requested: [Bug 312747] Include line break info for text using white-space: pre : [Attachment 199824] If text has computed style of white-space: pre, then don't expose
Aaron Leventhal (vacation: mostly gone until Oct 17) <aaronleventhal@moonset.net> has asked Boris Zbarsky <bzbarsky@mit.edu> for superreview: Bug 312747: Include line break info for text using white-space: pre https://bugzilla.mozilla.org/show_bug.cgi?id=312747 Attachment 199824: If text has computed style of white-space: pre, then don't expose https://bugzilla.mozilla.org/attachment.cgi?id=199824&action=edit ...

Reducing Web Page Size by Removing White Space
Hi,I have been having some performance issues with a website I am working on.  After some investigation I have discovered that there is a lot of whitespace in the pages of this site which is inflating the size of the pages being downloaded and impacting on performance.  I have been reading around this afternoon to try and find a simple solution to this problem without success.Is there an easy way of removing the whitespace from the aspx pages when publishing the website?I have seen a couple of methods in various forums that attempt to use Response.Filter to strip out the whitespace...

superreview requested: [Bug 191699] Make white-space work on inlines : [Attachment 248028] nsLineLayout changes to make white-space apply to inlines
David Baron <dbaron@mozilla.com> has asked Robert O'Callahan (Novell) <roc@ocallahan.org> for superreview: Bug 191699: Make white-space work on inlines https://bugzilla.mozilla.org/show_bug.cgi?id=191699 Attachment 248028: nsLineLayout changes to make white-space apply to inlines https://bugzilla.mozilla.org/attachment.cgi?id=248028&action=edit ------- Additional Comments from David Baron <dbaron@mozilla.com> There are still a bunch of trimming/collapsing issues before we pass Hixie's test, but this makes the result at least *resemble* the expected for...

superreview cancelled: [Bug 191699] Make white-space work on inlines : [Attachment 248028] nsLineLayout changes to make white-space apply to inlines
David Baron <dbaron@mozilla.com> has cancelled David Baron <dbaron@mozilla.com>'s request for superreview: Bug 191699: Make white-space work on inlines https://bugzilla.mozilla.org/show_bug.cgi?id=191699 Attachment 248028: nsLineLayout changes to make white-space apply to inlines https://bugzilla.mozilla.org/attachment.cgi?id=248028&action=edit ...

removing white spaces
Hi people! It sounds a bit stupid but I don't know the way to remove white spaces in a string. $string =3D "No s=E9 como quitar los putos spacios"; and now? thanks for your help Javier B. Try: $string =~s/ //g; That will get rid of all spaces. If you also want to get rid of tabs, $string =~s/[ \t]//g; Or you can use a \s instead of \t to also get rid of newlines, carriage returns and form feeds. Pete Hanson source1results.com -----Original Message----- From: softhome [mailto:javierbs@softhome.net] Sent: Monday, July 16, 2001 7...

remove white space
Dear all I'm creting a master page, I added HTML table then on the first row (merged columns) I added image on the second row on the left, for the menu i added another image my question is that I ave now white space between the form itself and the first row (the image ) and between the image and the menu please help me,  how to remove them  can you please post the HTML source Engineer Wahab HussainWeb DeveloperChillizone http://www.linkedin.com/in/hellowahab Hi,To skip blank space try it by changing width of your table to a %instead the px value like :-width="100&...

Removing White Space
I was wondering if anyone has ever experienced this problem.  I have a report that I created that is formatted in page set up to be in landscape.  In design view I have it so that it does not go past the end of the page and I know this because I cannot scroll over anymore and looking at the ruler its lined up at about 10.5.  When I look at my report in preview mode or through the crystal report viewer there is another whole page of white space to the right.  There is absolutely nothing in the white space.  If I print it prints all of my report on one page and then p...

Remove White Space
------=_NextPart_000_0011_01C11E92.B7458690 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit How can I remove white space from the beginning of a variable? -Scott ------=_NextPart_000_0011_01C11E92.B7458690-- substitute whitespace for nothing while matching the beginning of the scalar? On Mon, 6 Aug 2001, Scott Martin wrote: > How can I remove white space from the beginning of a variable? > > -Scott > > $var=~s/^\s*//; -----Original Message----- From: Scott Martin [mailto:smartin@nerds4rent.com] Sen...

removing white space
Is this correct, I want to remove leading and trailing white space, Thanks Dave -------------- foreach $item (param()) { my $value = param($item); $value =~ s/^\s+//; $value =~ s/\s+$//; } better?? foreach $item (param()) { my $value = param($item); $value =~ s/^\s+(.*)\s+$/$1/; } ------------- #!/usr/bin/perl use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use POSIX 'strftime'; $mailprog = '/usr/lib/sendmail'; # Retrieve Date $date = strftime('%A, %B %1d, %Y %I:%M %p',localtime) ,"\n"; # my %in = {}...

removing white space
Is the following regrex the correct way to remove leading or trailing white space from a string? $data = " bla@bla.net (mike smith) " $data =~ s/(^\s+)|(\s+$)//g; or would it be more efficient to it thus: # two passes $data =~ s/^\s+)//; $data =~ s/\s+$)//; Final comment when I am reading from a loop: open(EL,"$emailLog") || &errorLog( "Read in, Could not find $emailLog, $!"); my @lines = <EL>; foreach(@lines){ next if /test/; $_ =~ s/^\s+(.*)\s+$/\1,/; ..... other code.... Do I need to escape the '...

Removing empty white space
Hello, I have a webpage with a chart and gridview. In the page load event my chart and gridview is visible = false.  On top of my page I have a button.  When the button is pushed code is ran in the buttons click event.  The last few lines of code makes my chart and gridview visible = true. My problem is when my page first loads before the button is pushed I have a long empty page with vertical scroll bars.  I want to be able to remove the empty space when my page first loads. THANKS  Steve Did you try to put your chart and gridview in a  panel and then ...

removing white space #2
Hi, One part of my script ads several strings into one: $text = $part1.$part2.$part3.(...etc) However, if the part3 through part10 were empty I get that many white spaces at the end of $text. I thought the best thing would be just to remove the spaces at the end, but how? (maybe search and remove pattern?) thanks M _________________________________________________________________ Tired of spam? Get advanced junk mail protection with MSN 8. http://join.msn.com/?page=features/junkmail On Fri, 22 Nov 2002, Mariusz K wrote: > Hi, > > One part of my s...

Web resources about - Using Regular Expression Validator for removing consecutive white spaces with single white space - asp.net.web-forms

Most consecutive games with a touchdown pass (NFL) - Wikipedia, the free encyclopedia
Drew Brees holds or shares several records for consecutive games with a TD pass in NFL history, including 1+ reg. season TD passes, 1+ reg. and ...

SPOTLIGHT - Most consecutive pinky pull-ups - YouTube
Maibam Itomba Meitei performs the most consecutive pull-ups...using only his two pinkies! Check out his full attempt in this week's Spotlight. ...

Fourth consecutive day over 40C a record for Adelaide in December - AdelaideNow Search Search
... record, as the hot weather continues to take its toll across the state. The temperature hit 40.4C at 1pm on Saturday, marking the fourth consecutive ...

Family violence underpins crime increase for fifth consecutive year
In the year to June, almost half of crimes against Victorians were related to family violence, and authorities warn&nbsp;a plateau in the figures ...

Sebastian Vettel wins fourth consecutive world title: Video
Sebastian Vettel secures his fourth Formula One championship title after winning the Indian Grand Prix.

Thirty years of consecutive warmth: the heat really is on
... 30 years in which average monthly temperatures worldwide have been warmer than the average for the 20th century. century. That's 360 consecutive ...

Northern Ireland police face sixth consecutive day of riots in Belfast
LOYALIST protesters have attacked Northern Ireland police with petrol bombs, fireworks, bottles and stones during the sixth consecutive night ...

‘Air Bud’ Wins Best Picture for 18th Consecutive Year
Viewers of the 87th Academy Awards today were unsurprised to learn that the 1997 family comedy Air Bud took away its 18th consecutive Best Picture ...

Watch Steph Curry hit 43 consecutive three-point shots
To most Australians, Andrew Bogut is the most recognisable player in the NBA team, the Golden State Warriors. But the San Francisco-based side ...

Australian Stacey Keating wins second consecutive title
Australian Keating wins second consecutive title

Resources last updated: 1/6/2016 2:42:12 PM