Marc Rasmussen
Marc Rasmussen

Reputation: 20545

Check if a value contains a String character

Now i have a textfield and in javascript i wish to check if the value of this textfield is or contains a String.

I have tried the following:

      if(isNaN(parseInt($(this).val()))){}

Without effect.

ive also tried:

        if(typeof(parseInt($(this).val())) === "string"){}

ALTHOUGH these two examples might seem to work. They don't if you simply put the first character as a number then the rest of it wont matter.

So my question is how ?

Upvotes: 0

Views: 158

Answers (4)

VijithV
VijithV

Reputation: 41

if(typeof($(this).val()) == "string"){//is a string
    //do something with string
}else if(typeof($(this).val()) == "object"){//is a object
    if($(this).val().length != undefined){//is a array
       //do something with array
    }else{//is a object
      //do something with json object
    }
}

Upvotes: 0

chiliNUT
chiliNUT

Reputation: 19573

  if(!isNaN(parseInt($(this).val())) && $(this).val().length==parseInt($(this).val()).length) {}

Upvotes: 1

ams
ams

Reputation: 173

You can split the string into a character array and check each individually:

var s = "11test";
var split_s = s.split("");

for(var index=0;index<split_s.length;index++) {
    if(isNaN(parseInt(split_s[index]))){

    }
}

Upvotes: 1

adeneo
adeneo

Reputation: 318182

Maybe a regex to check if the value contains anything but a number :

/\D/.test(this.value)

FIDDLE

Upvotes: 1

Related Questions