2012-11-26 61 views

回答

1

如果您不使用插件,最简单的方法是编写正则表达式,并根据输入值匹配它。 如果找到一个非常非常好的一个here

$(function(){ 
    var strInput = $('input#myRomanInputField').val(); 
    var matchArr = strInput.match(/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/g); 
    console.log(matchArr); 
    if(matchArr) { 
     // test successful 
     console.log("true"); 
    } else { 
     // failure 
     console.log("false"); 
    } 
}); 

对于数字1-10,只需使用这个表达式:

/^(IX|IV|V?I{0,3})$|^X$/g