Validation for Non-Negative Integers and Decimal Values

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:

s: string;
i: Integer;

if InputQuery(Caption, 'Please enter a positive integer:', s) then
if TryStrToInt(s, i) and (i > 0) then
ShowMessage('An excellent choice, sir!')
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:

s: array of string;
i: Integer;
SetLength(s, 1);
if InputQuery(Caption, ['Please enter a positive integer:'], s,
function(const Values: array of string): Boolean
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?')
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+$) is negative lookahead to assert failure when input is non digit characters
  • ?:\.\d*)? allows for matching 123. 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