2013-12-18 4 views
2

아래의 시나리오를 확인하기위한 유효성 검사 기능이 있지만 함수는 맵 및 필터 메소드를 사용합니다. 그래서 its not working in IE8.지도 및 필터를 사용하지 않고이 기능을 다시 작성하는 방법.지도, 필터를 사용하지 않고 사용자 정의 Javascript 유효성 검사

var case1 = "stack(2),flow(2),over(4),temp(7)"; - true 
var case2 = "stack(2),flow(3),over(4),temp(k)"; - false 
var case3 = "stack(2),flow(2),over(4),temp(0"; - false 
var case4 = "stack(2),flow(2),over(,temp)"; - false 
var case5 = "stack(2),flow(2),over(4)temp(8)"; - false 
var case6 = "stack(1),flow(7),over,temp"; - true 
var case7 = "stack(1),flow(7),OVER,Temp"; - true 
var case8 = "stack(1),flow(7),over_r,temp_t"; - true 

자바 스크립트 기능 :

function validateChunk(s) 
{ 
    return !!s.match(/^[a-z]+(?:\(\d+\))?$/); 
} 

function filterValid(v) 
{ 
    return !v; 
} 

function testCases(str) 
{ 
    var chunks = str.split(","); 
    var validated = chunks.map(validateChunk); 
    return (0 === validated.filter(filterValid).length); 

} 

jsfiddle

답변

3

한 가지 가능한 방법은 루프에서 validateChunk 함수를 호출하는 것입니다

(!) 참고로
for (var i = 0, l = chunks.length; i < l; i++) { 
    if (! validateChunk(chunks[i])) { 
    return false; 
    } 
} 
return true; 

, 정확히 제로 의미있다 .test 클레어시 .match을 사용하십시오. rly는 (일치 결과를 수집 할 필요가 없기 때문에) 충분합니다. 이 모든 검증이 하나의 정규식으로 수행 될 수 있다는 것을 알아 차리지 도움이되지 수, 마지막으로

function validateChunk(str) { 
    return /^[a-z]+(?:\(\d+\))?$/.test(str); 
} 

: 그래서 이런 기능을 다시 것 사실

function testCases(str) 
{ 
    var pattern = /^[a-z]+(?:\(\d+\))?(?:,[a-z]+(?:\(\d+\))?)*$/ 
    return pattern.test(str); 
} 

, 당신의 설명에서 판단, 패턴은 약간 달라야합니다. 첫째, _ 심볼이 유효하며 문자 클래스에 포함되어야합니다. 둘째, 동일한 클래스는 A-Z 범위를 포함하거나 패턴 대신 /i 수정자를 지정해야합니다. This demo에는 이러한 모든 변경 사항이 포함되어 있습니다.

+0

한 사실 루프에 대한 원본보다 더 나은 솔루션이 될 것으로 보인다. – kapa

+0

위대한, 잘 작동합니다. 방법을 단순화시켜 주셔서 감사합니다. – user2848031

3

이러한 방법에 polyfill을 사용할 수 있습니다. 이러한 방법은 지원하지 않는 브라우저에서도 작동합니다. 나는 단순히 MDN에서 polyfills를 복사했습니다.

Array.prototype.map는 polyfill :

if (!Array.prototype.map) 
{ 
    Array.prototype.map = function(fun /*, thisArg */) 
    { 
    "use strict"; 

    if (this === void 0 || this === null) 
     throw new TypeError(); 

    var t = Object(this); 
    var len = t.length >>> 0; 
    if (typeof fun !== "function") 
     throw new TypeError(); 

    var res = new Array(len); 
    var thisArg = arguments.length >= 2 ? arguments[1] : void 0; 
    for (var i = 0; i < len; i++) 
    { 
     // NOTE: Absolute correctness would demand Object.defineProperty 
     //  be used. But this method is fairly new, and failure is 
     //  possible only if Object.prototype or Array.prototype 
     //  has a property |i| (very unlikely), so use a less-correct 
     //  but more portable alternative. 
     if (i in t) 
     res[i] = fun.call(thisArg, t[i], i, t); 
    } 

    return res; 
    }; 
} 

Array.prototype.filter polyfill는 :

if (!Array.prototype.filter) 
{ 
    Array.prototype.filter = function(fun /*, thisArg */) 
    { 
    "use strict"; 

    if (this === void 0 || this === null) 
     throw new TypeError(); 

    var t = Object(this); 
    var len = t.length >>> 0; 
    if (typeof fun != "function") 
     throw new TypeError(); 

    var res = []; 
    var thisArg = arguments.length >= 2 ? arguments[1] : void 0; 
    for (var i = 0; i < len; i++) 
    { 
     if (i in t) 
     { 
     var val = t[i]; 

     // NOTE: Technically this should Object.defineProperty at 
     //  the next index, as push can be affected by 
     //  properties on Object.prototype and Array.prototype. 
     //  But that method's new, and collisions should be 
     //  rare, so use the more-compatible alternative. 
     if (fun.call(thisp, val, i, t)) 
      res.push(val); 
     } 
    } 

    return res; 
    }; 
}