-2

$(document).ready(function() {


      Function arraysort(text) {

          $.ajax({
            type: "POST",
            url: "default.aspx/sort",
            data: JSON.stringify({
              arr: text.split(',')
            }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(response) {
              //some code here
            }
          });
          If

user give input into textbox as string then alert that please enter integer values and if it is an integer value then alert that input should be greater than one and integer should be comma seperated such as 100,23,12,1 and if single value or string or integers are not comma seperated then dont sort array .but here my textbox values are taking already string value and then passing this string value to the webmethod side.now how can I make sure that in textbox value should be integer but it already taking string value

ziel
  • 127
  • 2
  • 11

3 Answers3

0

Use typeof:

- typeof "foo"
"string"
- typeof true
"boolean"
- typeof 42
"number"

and put your logic with typeof to show different messages.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0
$(document).ready(function() {


  $("#btn").on("click", function() {
    text = $("#text1").val();
    if (typeof text != typeof 123){ alert("Please enter integer"); return;} // just add this line

    arraysort(text);
  });
});
Sagar V
  • 12,158
  • 7
  • 41
  • 68
  • but here my textbox values are taking already string value and then passing this string value to the webmethod side.now how can I make sure that in textbox value should be integer but it already taking string value – ziel Feb 06 '17 at 08:02
  • Sorry that i am not using c#. But in the above code, if it is not integer value, the value will not send to server. For that purpose we have given `return` statement. if it is a string, the funciton terminate – Sagar V Feb 06 '17 at 08:04
0

Client Side (Javascript and jQuery):

use isNaN it was designed for this

$(function() {
  $("#btn").on("click", function() {
    if (isNaN($("#text1").val())){
        alert("Text Value is Not a number");
        return;
    } else {
    // It's a number, add your code here
    }
  });
});

isNaN stand for "is not a number"

isNaN("text") // true
isNaN(1) // false
isNaN("1") // false

if you want to do Math with $("#text1").val() you can use Number($("#text1").val())

the code above will validate the value on client side and send to server only if valid

Server Side (C#):

as of your question on how to validate on server side using C# you can use

int n;
bool isNumeric = int.TryParse("123", out n);

also look here:

Community
  • 1
  • 1
Maher Fattouh
  • 1,742
  • 16
  • 24
  • but here my textbox values are taking string value so now how to validate input should be integer not string on server side – ziel Feb 06 '17 at 09:46
  • 1
    I don't see a need for server-side validation since your code will not send invalid values anyway if you use the method I described, reading the comments I read you're using c# so I attached some links on how to do the same using it – Maher Fattouh Feb 06 '17 at 09:58
  • Thanks @Maher isNan worked for me and I used some regx for comma purpose so it worked for me – ziel Feb 08 '17 at 11:12