validation on weight in asp.net? - asp.net

can i get validation on weight which can take decimal values and also integer.
suppose if i enter 60 it has to accept and if i enter 60.50 it has to accept (60.1..etc)
but not characters. Thank you.

Use regular expression validator to constrain input.
Positive Decimal: (^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)
How To: Use Regular Expressions to Constrain Input in ASP.NET

you can use compare validator and use type double and operator data type check. it will work for you.

Ok try number two since I misunderstood your question :)
Use a RegularExpressionValidator and use the following expression:
^\d+\.?\d*$
Seems to match any number with as many decimal places as you allow.

Related

ASP.NET default regular expression for users' passwords

What is the default regular expression used by asp.net create user wizard?
According to the MSDN documentation, it should be something like this:
Regular Expression: #\"(?:.{7,})(?=(.*\d){1,})(?=(.*\W){1,})
Validation Message: Your password must be 7 characters long, and contain at least one number and one special character.
However, it does not work as it does not accept something like 3edc£edc, which is actually accepted when using the default create user wizard.
Any idea about how can I get this regular expression?
The error is in the ?: in (?:.{7,})(?=(.*\d){1,})(?=(.*\W){1,}) that is "consuming" the fist seven characters or more characters. It should be ?= OR you can invert the order: (?=(.*\d){1,})(?=(.*\W){1,})(?:.{7,})
Just change the order
^(?=(.*\d))(?=(.*\W)).{7,}
I additionally removed the {1,} and anchored it to the start of the string and you don't need a group around the last part
See it here on Regexr

Validation for TextBox For a User Form

In my User Registration Form, I want my user address text box to accpet alphanumeric as well as special character but should not contain only numeric or special character.
I am currently using regular expression validator ,what regular expression i can use.
Or Is there any different solution for that.
Regards
Here you go.. I have used it at one place, and works pretty well.. just provide your acceptable characters and you are good to go
http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/FilteredTextBox/FilteredTextBox.aspx
You mean from your question that a string of alphabets is a must right ?

Using a Regular Expression Validator for numbers in a textbox

How can I use a Regular Expression Validator to ensure that only a number like 3.00 is entered into a text box?
You could use the CompareValidator with type="Double" to allow numbers only.
Using the RegularExpressionValidator you could use this ValidationExpression: \d+\.\d{2} (One or more digits, a decimal point, two digits.)
I always use this nice little tool to compose regular expressions.
Does this answer your question or do you need help on how to use the RegularExpressionValidator control in general?
If you want to verify those cases like 123, 123., 123.12, and .23, the following may be used:
ValidationExpression="(\d+\.\d+)|(\d+\.)|(\.\d+)|(\d+)"

Regex to limit string length for strings with new line characters

Looks like a simple task - get a regex that tests a string for particular length:
^.{1,500}$
But if a string has "\r\n" than the above match always fails!
How should the correct regex look like to accept new line characters as part of the string?
I have a <asp:TextBox TextMode="Multiline"> and use a RegularExpressionValidator to check the length of what user types in.
Thank you,
Andrey
You could use the RegexOptions.Singleline option when validating input. This treats the input as a single line statement, and parses it as such.
Otherwise you could give the following expression a try:
^(.|\s){1,500}$
This should work in multiline inputs.
Can you strip the line breaks before checking the length of the string? That'd be easy to do when validating server-side. (In .net you could use a custom validator for that)
From a UX perspective, though, I'd implement a client-side 'character counter' as well. There's plenty to be found. jQuery has a few options. Then you can implement the custom validator to only run server-side, and then use the character counter as your client-side validation. Much nicer for the user to see how many characters they have left WHILE they are typing.
The inability to set the RegexOptions is screwing you up here. Since this is in a RegularExpressionValidator, you could try setting the options in the regular expression itself.
I think this should work:
(?s)^.{1,500}$
The (?s) part turns on the Singleline option which will allow the dot to match every character including line feeds. For what it's worth, the article here also lists the other RegexOptions and the notation needed to set them as an inline statement.

Text box validataion for Decimal value

I have an application in which I want to validate a text box that it will take decimal value.
I would like to use a regular expression validator for this. What would this expression look like?
I wouldn't use a regular expression - create a custom validator that uses Decimal.TryParse under the covers.
Edit: To be fair to the question, here is a regular expression that would do the trick:
^\d*\.?\d+$
just the regex would be
`\d*`
I'd use a CompareValidator - for type decimal
And a Required field Validator - so blank is not allowed
But regular expression is accepable this link gives some examples http://msdn.microsoft.com/en-us/library/ms998267.aspx
I say to keep using the Regular Expression Validator, you'll get the added benefit of client-side validation with it.
Here is a list of regular expressions related to decimals:
http://regexlib.com/DisplayPatterns.aspx?cattabindex=2&categoryId=3
I would use a CompareValidator and use Regular Expression
^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$
This allow all decimal number, exclude all alphanumeric caracter
e.g.
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="Only Decimal Value"
ValidationExpression="^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$">
</asp:RegularExpressionValidator>

Resources