簡體   English   中英

我如何使文本框接受數字后跟+號或-號

[英]how i can make textbox accept number followed by + sign or - sign

我希望阻止使用用戶輸入除以數字開頭,以+號或-號結尾的文本以外的任何文本嗎?

您可以嘗試以下方法:

^\d.*[+-]$

演示版

樣本來源:( 在此處運行

    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");

您可以在輸入的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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM