Click here to Skip to main content
15,033,147 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
heyy...


I want a Regular Expression for integer(that greater than 0 ) and should not be empty.

if empty or greater than 0 then show error msg..... .I tried but cannot find both expression together ....Any ideaa???
Posted
Comments
JoCodes 9-Oct-13 5:22am
   
Have you tried javascript validations for the same instead of regularexpression validation control;?
Neema Derakhshan 9-Oct-13 5:25am
   
you should use a RequiredFieldValidator to check if it is empty and a RegularExprssionValidator to check if it is greater than 0
aravindnass 9-Oct-13 5:44am
   
In required field validator I set initial value to 0 , but if 0 is removed the press button ,the validation not work.....? Any solution

Regular expressions are not good for "greater than" values - they don't know about numbers at all, so the expression becomes rather clumsy:
^0*[1-9]\d*$
You would be better doing this kind of validation in code, rather than as a regex.
   
Hi, If you are doing it using Asp.net text box, then you can use RequiredFieldValidator and RegularExpressionValidators for the same text box. Regular Expression for input value more than 1 is "^[1-9]*".
You code should look like:
XML
<asp:TextBox ID="MyTextBox" runat="server"></asp:TextBox>
        <asp:RegularExpressionValidator ControlToValidate="MyTextBox" ID="regExVal" runat="server" ErrorMessage="Provide value more than 1." ValidationExpression="^[1-9]*"/>
        <asp:RequiredFieldValidator ControlToValidate="MyTextBox" ID="reqVal" runat="server" ErrorMessage="Provide some value."/>


Hope this will help.
   
v2
Comments
Matt T Heffron 9-Oct-13 13:04pm
   
Your regex is not quite right.
Yours will fail on: 10, 100, 203, (i.e., any valid integer that contains a zero)
It also will not ignore leading zeros, and it will succeed on an empty string.
See Griff's regex in Solution 2.
On Submit button click, call a Js function as below:

C#
<asp:textbox id="txtNumber" runat="server" xmlns:asp="#unknown"></asp:textbox>

protected void Submit(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"alert","CallMe();",true);
}


Js function:

JavaScript
function CallMe()
{
var e=document.getElementById('txtNumber').value;
if(e=='' || e>0)
{
alert('Invalid Number, Please enter the number again');
}
}
   
Suggest you to use a Custom validator control if you intent to use a single control for regular expression as well as Required value to be validated. Try something like below.

XML
<asp:CustomValidator ID="CustomValidator1" ValidateEmptyText="true"  ClientValidationFunction="ClientValidation"
ControlToValidate="TextBox1" OnServerValidate="ServerValidation"  runat="server" ErrorMessage="Errorrrr"></asp:CustomValidator

>

Add Clientside script as

XML
<script lang="javascript" type="text/javascript">
       function ClientValidation(source, arguments) {
           alert("Hi");
           if ((arguments.Value % 2) == 0 && arguments.Value >0)
               arguments.IsValid = true;
           else
               arguments.IsValid = false;
       }
</script>


Add a server side method for cross check if needed .

C#
public void ServerValidation (object source, ServerValidateEventArgs arguments)
        {
            if(!string.IsNullOrEmpty(arguments.Value))
            {
                int i = int.Parse(arguments.Value);
                arguments.IsValid = ((i%2) == 0);
            }
            else
            {
                arguments.IsValid = false;
            }
        }


Hope this helps
   
v2
use this java script
C#
function Validate()
{
var e=document.getElementById('txtNumber').value;
if(e=='' || e>0)
{
alert('Invalid Number, Please enter the number again');
}
}

<asp:textbox id="MyTextBox" runat="server" onkeyuo="return Validate()" >
</asp:textbox>
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900