2013-12-13 3 views
0

텍스트 필드에 입력되는 변수 유형을 정의하는 방법은 무엇입니까? 검색 버튼을 누를 때 텍스트 필드 안에 내용을 입력하고 싶습니다. 유형에 따라 버튼은 데이터베이스와 다른 결과를 보여야합니다!삽입 할 유형 정의 문자열 또는 int

if(numbers 0-9){ 
    //do something 
} 
else if (letters A-Z){ 
    //do something else 
} 

어떻게 자바 스크립트에서 그렇게할까요?

+1

[. 정규 표현식 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) – Pointy

+1

번호가있는 경우는 어떻게 *와 * 문자? (당신이 * 이것을 허용하기를 원하지 않는다 할지라도, 그것을 준비하십시오; 왜냐하면 그것은 우연히 만 일어날 것이기 때문입니다.) –

답변

3

대부분의 당신은 수행하여 정수로 문자열을 변환 할 수 있습니다

function isNumber(n) { 
    return !isNaN(parseFloat(n)) && isFinite(n); 
} 
0

로 이동합니다

Boolean(+a) is true then a is Number else it's not a number 
0

당신은 방법을 시도 할 수 있다면

var a = "3"; 
var b = +a; // this will try to convert a variable into integer if it can't it will be NaN 

당신이 확인할 수 있습니다 toType 앵거스 크롤이 정의한이 blog post :

var toType = function(obj) { 
    return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase() 
} 

구현 :

toType({a: 4}); //"object" 
toType([1, 2, 3]); //"array" 
(function() {console.log(toType(arguments))})(); //arguments 
toType(new ReferenceError); //"error" 
toType(new Date); //"date" 
toType(/a-z/); //"regexp" 
toType(Math); //"math" 
toType(JSON); //"json" 
toType(new Number(4)); //"number" 
toType(new String("abc")); //"string" 
toType(new Boolean(true)); //"boolean" 
관련 문제