2017-09-24 52 views

回答

0

你可以試試這個:

^\d.*[+-]$ 

Demo

樣品來源:(run here

string pattern = @"^\d.*[+-]$"; 
    string input = @"1adfasdf-"; 

    Regex regex = new Regex(pattern); 
    Match match = regex.Match(input); 
    if (match.Success) 
     Console.WriteLine(match.Value); 
    else 
     Console.WriteLine("clear the input field and show an error message if you want"); 
+0

但是,這也將匹配像'12waew +'這樣的字符串。不確定OP是否需要這樣 – Gurman

+1

op沒有提及第一個數字和最後一個符號之間會出現什麼,因此它被認爲是通配符 –

0

您可以設置正則表達式在輸入的pattern屬性,並設置關於title屬性的錯誤消息。你的案例的正則表達式應該是[0-9].*[+-]

<form> 
 
    <input type="text" title="The text must start with a number and end with a + or -" pattern="[0-9].*[+-]" /> 
 
    <button type="submit">Submit</button> 
 
</form>

相關問題