views:

98

answers:

5

I'm trying to validate that a user only enters a long value as input (long bigger than 0 actually).

Compare and Range validator has DataTypeCheck for int values only. I was planning on using this class in a CustomValidator but then I would need to write both, client and server side validation code.

Do you know of any other good way of doing this? Thanks!

A: 

you can try AJAX control FilteredTextBox

http://www.asp.net/AJAX/AjaxControlToolkit/Samples/FilteredTextBox/FilteredTextBox.aspx

Muhammad Akhtar
A: 

//Server side

function Boolean isValid(){
  try{
    long a=long.Parse(textbox1.Text);
    if(a>0) 
      return true;  
    return false;
  }
  catch (Exception exp)
  {
    return false;  
  }
}

// Client Side

use parseLong() function instead of long.Parse(). Otherwise same as server side

ebattulga
A: 

It looks like the Compare and Range Validators will work with longs. Setting the type to "Integer" will allow it to be valid for longs as well.

For server side validation, just use if(Page.IsValid){...} which will trigger the appropriate validation.

Chris
A: 

Server Side

Boolean IsLong(String input)
{
    Int64 r;
    return Int64.TryParse(input, out r);
}

Client Side

function isLong(field) {    
    field.value = field.value.replace(/[^0-9]/, '');   
    return (field.value.length < 19);
}
ChaosPandion
A: 

Use a RegularExpressionValidator with an expression of

"^\d*[1-9]\d*$"

This will validate that it is any number of digits with at least one 1-9, (so greater than zero).

eidylon