[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need to trim space in the regular expression

Posted on 2008-11-10
12
Medium Priority
?
544 Views
Last Modified: 2013-12-17
Hi,
Am using regular expression validator , for to accept only numbers .  that validator function is
((0)+(\.[1-9](\d)?))|((0)+(\.(\d)[1-9]+))|(([1-9]+(0)?)+(\.\d+)?)|(([1-9]+(0)?)+(\.\d+)?)
1. Accept only numbers and also in decimal
2.It doesnot accept zero i.e greater than 0 only can accept,

Problem:
Eg:i.123(if space given ) Error message of validator is popping out .  
     ii,(space)12(space)3 Error message of validator is popping out .  

I need a expression , which should trim empty spaces also.
0
Comment
Question by:Rajar Ahmed
  • 7
  • 5
12 Comments
 
LVL 7

Expert Comment

by:aherps
ID: 22927552
You could just do a string replace on the input string:


inputString = inputString.Replace(" ","");

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22927581
hi, where should be added this code..
in aspx r source code..???
<tr>
                <td style="width: 100px">
                    Enter Price<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
                        ControlToValidate="txtfprice" ErrorMessage="*" Style="position: relative; left: 1px; top: 0px;"></asp:RequiredFieldValidator></td>
                <td style="width: 100px">
                    <asp:TextBox ID="txtfprice" runat="server" Style="position: relative"></asp:TextBox></td>
                <td style="width: 100px">
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtfprice"
                        ErrorMessage="Enter in Number only" Style="position: relative" ValidationExpression="((0)+(\.[1-9](\d)?))|((0)+(\.(\d)[1-9]+))|(([1-9]+(0)?)+(\.\d+)?)|(([1-9]+(0)?)+(\.\d+)?[ \t]+$)"></asp:RegularExpressionValidator></td>
            </tr>

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22927585
my aspx code
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection con;
        string constr = ConfigurationManager.AppSettings["cons"];
        con = new SqlConnection(constr);
        con.Open();
        
        SqlParameter foodprice = new SqlParameter("@price", SqlDbType.Float, 4);
        foodprice.Value = txtfprice.Text;
        SqlParameter foodquantity = new SqlParameter("@quantity", SqlDbType.Int, 4);
        
        cmd1.Parameters.Add(foodprice);
 
        int comment = cmd1.ExecuteNonQuery();
        if (comment == 1)
        {
 
            Label1.Text = "Your Value is Added";
        }
        else
        {
 
            Label1.Text = "Already Exists";
        }
        con.Close();
       
    }

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:aherps
ID: 22936960
You could allow spaces in the Validation input, and just parse the string replace at:

foodprice.Value = txtfprice.Text.Replace(" ","");
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22937102
this is how i changed , but still am gettin the error message of regex validator of that control..
SqlParameter foodprice = new SqlParameter("@price", SqlDbType.Float, 4);
foodprice.Value = txtfprice.Text.Replace(" ", "");

Open in new window

0
 
LVL 7

Expert Comment

by:aherps
ID: 22937254
You would still need to allow the validator to accept '\s' to allow the above code to work...

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22937278

wel, how can i alllow .. . ?????


this is my req ex validator  code...Is there any modificationn required  ..????

Or in aspx  source  code....

If any ,  plz highlight the changes in code ...

My Expression
((0)+(\.[1-9](\d)?))|((0)+(\.(\d)[1-9]+))|(([1-9]+(0)?)+(\.\d+)?)|(([1-9]+(0)?)+(\.\d+)?)
 
 
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtfprice"
                        ErrorMessage="Enter in Number only" Style="position: relative; left: -235px; top: 0px;" ValidationExpression="((0)+(\.[1-9](\d)?))|((0)+(\.(\d)[1-9]+))|(([1-9]+(0)?)+(\.\d+)?)|(([1-9]+(0)?)+(\.\d+)?[ \t]+$)" Height="16px" Width="1px">*</asp:RegularExpressionValidator></td>

Open in new window

0
 
LVL 7

Expert Comment

by:aherps
ID: 22937311
If you're just trying to match numbers above 0 - you could change it to:

^[\s]|[1-9]\d*?[0]|[\s]*$

It allows spaces also. - You can strip them out after in the replace function in .vb code as you have done
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22937335
this regex not even accepting numbers ..
but i want a regex which  can accept numbers,decimal and truncate  empty spaces

0
 
LVL 7

Accepted Solution

by:
aherps earned 2000 total points
ID: 22937359
Whoops - sorry mate:
Try this:
^([1-9]\d*|\s)*(\.?([0-9]|\s)*)$

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22937376
it works......

Thanks a lot  mate....
0
 
LVL 18

Author Closing Comment

by:Rajar Ahmed
ID: 31515380
good
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question