2009-08-05 74 views
1

我需要通過JavaScript中的正則表達式驗證下面的範圍。Javascript正則表達式對初學者的幫助

-20.00至+20.00,增量爲0.5。

用戶應該能夠輸入1.50而不是+01.50。

我試圖在谷歌做很多研究,雖然正則表達式並不容易。如果我理解正確,那麼我將需要將正則表達式分解爲更小的組。從程序員的角度來看,我會認爲,我需要驗證每個鍵入輸入字段的「字符」。如果我理解如何分組在正則表達式中工作,那麼我想我可以自己做正則表達式。所以,請幫助我瞭解上述問題的正則表達式,不只是扔在正則表達式:)

感謝,

回答

1

下面應該這樣做

var myregexp = /^[+-]?(20\.00|[01?][0-9]\.[05]0)$/m; 
if (subject.match(myregexp)) { 
    // Successful match 
} else { 
    // Match attempt failed 
} 

// ^[+-]?(20\.00|[01?][0-9]\.[05]0)$ 
// 
// Options:^and $ match at line breaks 
// 
// Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» 
// Match a single character present in the list below «[+-]?» 
// Between zero and one times, as many times as possible, giving back as needed (greedy) «?» 
// The character 「+」 «+» 
// The character 「-」 «-» 
// Match the regular expression below and capture its match into backreference number 1 «(20\.00|[01?][0-9]\.[05]0)» 
// Match either the regular expression below (attempting the next alternative only if this one fails) «20\.00» 
//  Match the characters 「20」 literally «20» 
//  Match the character 「.」 literally «\.» 
//  Match the characters 「00」 literally «00» 
// Or match regular expression number 2 below (the entire group fails if this one fails to match) «[01?][0-9]\.[05]0» 
//  Match a single character present in the list 「01?」 «[01?]» 
//  Match a single character in the range between 「0」 and 「9」 «[0-9]» 
//  Match the character 「.」 literally «\.» 
//  Match a single character present in the list 「05」 «[05]» 
//  Match the character 「0」 literally «0» 
// Assert position at the end of a line (at the end of the string or before a line break character) «$» 
0

可能是更簡單的方法來檢查你有2個有效數字浮點數(正則表達式是好的,但也可以使用parseFloat),並在最後一步檢查programmaticly,這些數字是在範圍[-20.0 - 20.0]

0

你想要的正則表達式是:

/[-+]?([01]?[0-9](\.[05]0?)?|(20(\.00?)?)/ 

這種分解成:

[-+]? : an optional - or + 
( 
[01]? : an optional 0 or 
[0-9] : number 0-9 
(...)? : Optional 
    \. : . 
    [05] : 0 or 5 
    0? : Optional 0 

    |  : OR 

    20  : numbers 20 
    (...)? : Optional 
    \. : . 
    00? : O or OO 
) 

注意這將允許「-00.00」

+0

小毛刺,但這個表達式也匹配20.05& - 20.05 – 2009-08-05 12:31:57

+0

更新d根據評論 – Callum 2009-08-05 12:43:21

4

它並沒有真正意義的使用正則表達式驗證數值的偏僻角落裏的情況。

嘗試:

function checkRange(input) { 
    var value = parseFloat(input); 
    return (!isNaN(value) && (value >= -20) && (value <= 20) && (value % 0.5 == 0)); 
} 
+0

我想我需要確保用戶以正確的格式輸入值。 – vikasde 2009-08-05 13:31:49

0

這應該分兩個步驟來完成。第一步是使用正則表達式來確保輸入的值是文本有效的(即它有x十進制數,它有一個符號等)。然後將其轉換爲值以確保它是邏輯上的有效。

所以首先,你可能需要一個正則表達式是這樣的:

/^[+-]?\d{1,2}\.(00|50)$/ 

它會:

  • 確保字符串可選開始+-
  • 後面跟着一個或兩個數字。
  • 後面跟着一個點。
  • 通過0050

之後隨之而來的,可以保證它在-20至20的範圍內:

var floatValue = Number(value); 
if (floatValue < -20 || floatValue > 20) { 
    // Invalid! 
} 
0

如前所述,範圍使用數字更加匹配,但使用正則表達式來確保小數點後的格式是正確的。

var value = ... // eg. "-20.05" 
var number = parseFloat(value); 
if (number >= -20 && number <= 20 && /[+-]?\d+\.[05]0/.test(value)) { 
    alert("in range") 
} else { 
    alert("out of range") 
} 
0

以下的正則表達式應該工作:

^[+-]?(?:20\.?0{0,2}?|[01]?[0-9]\.?[05]?0?)$ 

說明:

^        # anchor the match at the start of the string 
    [+-]?      # optional sign 
    (?:      # non-capturing group for alternation: 
     20\.?0{0,2}?   # match either 20 (optionally followed by . or .0 or .00) 
    |       # or 
     [01]?[0-9]\.?[05]?0? # match 00-19 (optionally followed by . or .0 or .5 or .50 or .00) 
    )       # end of alternation 
$        # anchor the match at the end of the string