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.

💬 15 Replies
👁️‍🗨️ 1286 Views

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: