2013-06-25 10 views
10

RegularExpression(@"^\d{1,15}$")] का उपयोग करना, मैं उपयोगकर्ता 15 लंबाई में है, जो त्रुटि संदेश देता है के लिए अंक दर्ज करना चाहते हैं 'एक संपर्क नंबर के लिए 15 अंक से ऊपर दर्ज करें' यदि यह सही नहीं हैनियमित अभिव्यक्ति त्रुटि संदेश

[Required(ErrorMessage = ("Please enter up to 15 digits for a contact number")), Display(Name = "Contact Number"), RegularExpression(@"^\d{1,15}$")] 
public string ContactNumber { get; set; } 

उपयोगकर्ता इस पर मुझे त्रुटि संदेश के साथ छोड़ दिया हूँ करने के लिए विफल रहता है:

The field Contact Number must match the regular expression '^\d{1,15}$'. 

'Please enter up to 15 digits for a contact number' के बजाय ... किसी को पता है क्यों? धन्यवाद

उत्तर

30

आपने ErrorMessage को RequiredAttribute पर असाइन किया है (जिसे नियमित रूप से अभिव्यक्ति के कारण आपको इस मामले में बिल्कुल आवश्यकता नहीं है)। तो:

[Display(Name = "Contact Number")] 
[RegularExpression(@"^\d{1,15}$", ErrorMessage = "Please enter up to 15 digits for a contact number")] 
public string ContactNumber { get; set; } 
+0

मदद लोगों के लिए बहुत धन्यवाद – John

4

आप RegularExpression विशेषता, नहीं Required विशेषता में अपना संदेश की जरूरत है।

आपने अपना त्रुटि संदेश Required विशेषता में जोड़ा है, जिसका अर्थ है कि यह फ़ील्ड खाली होने पर प्रदर्शित होगा।

संबंधित मुद्दे