Editor Validation: How to enforce integer numbers

Editor Validation: How to enforce integer numbers

rdmrdm Posts: 194Questions: 55Answers: 4

I am examining the .NET Validation page and see how enforce use of a decimal number, but I don't see any examples of enforcing an integer number.

What i hope to do is something like:

.Field(new Field("Table1.Column3")
.Validator(/* number must be int; number can be null */))

Is this possible?

This question has an accepted answers - jump to answer

Answers

  • rdmrdm Posts: 194Questions: 55Answers: 4

    So after tinkering around some more, I came up with this. I observed that because the column is defined as an int on the database, any decimal number is truncated -- not rounded. The behavior works as a failsafe and is better than nothing. What else do I need to ensure that the user is blocked from entering a decimal number the same way the user is blocked from entering a string?

    .Field(new Field("Table1.Column3")
       .Validator(Validation.Numeric(
          "en-US",
          new ValidationOpts
          {
             Empty=true,
             Message="This must be a whole number"
          }))
       .SetFormatter(Format.NullEmpty()))
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    This thread from yesterday will probably be of some interest to make sure the user enters a number.

    Allan

  • rdmrdm Posts: 194Questions: 55Answers: 4

    I tried that. All this code did was add up/down arrows to the text box.

    attr: {
       type: "number"
    }
    

    If I entered a decimal number, the value was then truncated and the record updated. There was no message that an integer needed to be entered.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Looks like that's the best HTML can provide just now.

    You'd need to use a validator on the server-side to determine if it is a floating point value or not and reject it if so.

    The only other option on the client-side i can think of is to add a key event listener and block anything other than numbers.

    Allan

  • rdmrdm Posts: 194Questions: 55Answers: 4

    I was hoping I wasn't missing something obvious. At least numeric behavior is enforced. It appears that SQL Server might be doing the truncation, so I can make sure that incorrect data types are not entered.

This discussion has been closed.