How to validate values that must be integer, non negative, non decimal and not a string? (Or an alphabetic word) from an InputQuery?
This is easy:
var
s: string;
i: Integer;
begin
if InputQuery(Caption, 'Please enter a positive integer:', s) then
if TryStrToInt(s, i) and (i > 0) then
ShowMessage('An excellent choice, sir!')
else
ShowMessage('That''s not a positive integer, is it?')
This will display the prompt and do nothing if the user cancels it (that's expected). On the other hand, if the user does enter a value, it is verified using TryStrToInt
and a simple sign test.
Notice that, due to boolean short-circuit evaluation, the second conjunct (i > 0
) will only evaluate if the first conjunct (TryStrToInt(s, i)
) evaluates to True
, so we will never test an uninitialized variable i
(not that it would matter in this case, though).
You may also want to use a more sophisticated input box that automatically validates the input within the dialog itself (disclaimer: my site).
Alternatively, you can use the InputQuery
function's own validation feature, which will validate the input when the user clicks OK:
var
s: array of string;
i: Integer;
begin
SetLength(s, 1);
if InputQuery(Caption, ['Please enter a positive integer:'], s,
function(const Values: array of string): Boolean
begin
Result := (Length(Values) = 1) and TryStrToInt(Values[0], i) and (i > 0);
if not Result then
ShowMessage('That''s not a positive integer, is it?')
end)
then
ShowMessageFmt('You chose %d. That''s an excellent choice, sir!', [i]);
Regex to validate a .NET's non-negative decimal or blank?
You can use this regex based on a negative lookahead to avoid matching single dot:
^(?!\D+$)\+?\d*?(?:\.\d*)?$
(?!\D+$)
is negative lookahead to assert failure when input is non digit characters?:\.\d*)?
allows for matching123.
type of number.
RegEx Demo 1
RegEx Demo 2
jquery - allow only negative, positive or decimal number validation in field
You can use regex instead to solve your problem, change your method to something like this:
function isNumbers(evt, element)
{
var elementValue = $(element).val();
var regex = /^(\+|-)?(\d*\.?\d*)$/;
if (regex.test(elementValue + String.fromCharCode(evt.charCode))) {
return true;
}
return false;
}
Validating a percentage variable. No decimal points, No negative numbers
A Mod check will work to make sure that the variable is a whole number:
If Pcomplete % 1 == 0
, then it is not a decimal number.
Also, the vairbale cannot be both over 100 and less than 0, so seperate them with a ||.
if ((Pcomplete == "" ) || (Pcomplete < 0) || (Pcomplete > 100) || (PComplete % 1 !==0) {
alert('Percentage field is required. You need to type a whole number between 0 and 100');
return false;
}
How to make type=number to positive numbers only
Add a min
attribute
<input type="number" min="0">
Validate positive integers
Looks like you are looking for natural numbers, I think you can use the regex pattern to get the desired output. Something like
@Pattern(regexp = "[\\s]*[0-9]*[1-9]+",message="msg")
Related Topics
How to 'Validate' on Destroy in Rails
Rails: Activerecord Query Based on Association Value
Ruby 1.9: How to Properly Upcase & Downcase Multibyte Strings
How to Force Rails_Env in a Rake Task
What's the Difference Between a String and a Symbol in Ruby
"Bin/Rails: No Such File or Directory" W/ Ruby 2 & Rails 4 on Heroku
Regular Expressions With Validations in Ror 4
How Does the Magic Comment ( # Encoding: Utf-8 ) in Ruby Work
I Don't Understand Ruby Local Scope
Weird Backslash Substitution in Ruby
How to Test For (Activerecord) Object Equality