Need to trim space in the regular expression

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.
LVL 18
Rajar AhmedConsultantAsked:
Who is Participating?
 
aherpsConnect With a Mentor Commented:
Whoops - sorry mate:
Try this:
^([1-9]\d*|\s)*(\.?([0-9]|\s)*)$

0
 
aherpsCommented:
You could just do a string replace on the input string:


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

Open in new window

0
 
Rajar AhmedConsultantAuthor Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Rajar AhmedConsultantAuthor Commented:
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
 
aherpsCommented:
You could allow spaces in the Validation input, and just parse the string replace at:

foodprice.Value = txtfprice.Text.Replace(" ","");
0
 
Rajar AhmedConsultantAuthor Commented:
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
 
aherpsCommented:
You would still need to allow the validator to accept '\s' to allow the above code to work...

0
 
Rajar AhmedConsultantAuthor Commented:

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
 
aherpsCommented:
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
 
Rajar AhmedConsultantAuthor Commented:
this regex not even accepting numbers ..
but i want a regex which  can accept numbers,decimal and truncate  empty spaces

0
 
Rajar AhmedConsultantAuthor Commented:
it works......

Thanks a lot  mate....
0
 
Rajar AhmedConsultantAuthor Commented:
good
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.