È possibile utilizzare le NoteAnnotations di ASP.NET MVC 2 per consentire solo caratteri (nessun numero) o persino fornire una lista bianca di stringhe consentite? Esempio?DataAnnotations - Disallow Numbers o consenti solo stringhe date
12
A
risposta
27
Utilizzare RegularExpressionAttribute.
Qualcosa di simile
[RegularExpression("^[a-zA-Z ]*$")]
dovrebbe corrispondere a-z maiuscole e struttura inferiore.
Una lista bianca sarebbe simile
[RegularExpression("white|list")]
che dovrebbe consentire solo "bianco" e "lista"
[RegularExpression("^\D*$")]
\ D rappresenta i caratteri non numerici in modo quanto sopra dovrebbe consentire una stringa con tutto tranne 0-9.
Le espressioni regolari sono difficili, ma ci sono alcuni strumenti di test utile online come: http://gskinner.com/RegExr/
3
Sì. Utilizzare "[espressione regolare]"
Questo è un ottimo sito su espressioni regolari http://www.regexlib.com/CheatSheet.aspx
1
È possibile scrivere il proprio validatore che ha prestazioni migliori rispetto un'espressione regolare.
Qui ho scritto un validatore whitelist oggetti int:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace Utils
{
/// <summary>
/// Define an attribute that validate a property againts a white list
/// Note that currently it only supports int type
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
sealed public class WhiteListAttribute : ValidationAttribute
{
/// <summary>
/// The White List
/// </summary>
public IEnumerable<int> WhiteList
{
get;
}
/// <summary>
/// The only constructor
/// </summary>
/// <param name="whiteList"></param>
public WhiteListAttribute(params int[] whiteList)
{
WhiteList = new List<int>(whiteList);
}
/// <summary>
/// Validation occurs here
/// </summary>
/// <param name="value">Value to be validate</param>
/// <returns></returns>
public override bool IsValid(object value)
{
return WhiteList.Contains((int)value);
}
/// <summary>
/// Get the proper error message
/// </summary>
/// <param name="name">Name of the property that has error</param>
/// <returns></returns>
public override string FormatErrorMessage(string name)
{
return $"{name} must have one of these values: {String.Join(",", WhiteList)}";
}
}
}
campione Uso:
[WhiteList(2, 4, 5, 6)]
public int Number { get; set; }
Problemi correlati
- 1. Confronta date DataAnnotations convalida asp.net mvc
- 2. Disallow o Noindex sul sottodominio con robots.txt
- 3. Consenti stringhe vuote per i campi contrassegnati con PhoneAttribute o UrlAttribute
- 4. Consenti solo numeri e punto nello script
- 5. Stringhe di date scadenti?
- 6. NSDecimalNumber round long numbers
- 7. Consenti solo un'istanza di script python?
- 8. DataAnnotations o blocco di convalida applicazione
- 9. Objective C - Array With Numbers
- 10. Robots.txt: consenti solo i principali SE
- 11. Consenti solo caratteri alfanumerici per un UITextField
- 12. Consenti richiesta proveniente solo da IP specifico
- 13. Swift - Consenti la rotazione solo su iPad
- 14. Js Numbers - frazioni
- 15. Haskell Peano Numbers
- 16. formattazione virgola mobile Numbers
- 17. Disallow mix di tratti specifici
- 18. Date due stringhe, è uno l'anagramma dell'altro
- 19. Confronto di stringhe di date con date effettive in Scala
- 20. ErrorMessage viene ignorato in DataAnnotations DataType attributo
- 21. Uso DataAnnotations 4,0
- 22. Consenti solo a Google CSE e non consente la ricerca standard di Google in ROBOTS.txt
- 23. Consenti solo a determinati domini di poter caricare un iFrame
- 24. convert date date aaaammgg a aaJJJ o Julian Date
- 25. L'attributo DataAnnotations [Telefono]
- 26. DataAnnotations - Convalida non valida
- 27. Consenti solo l'immissione di dati univoci con Android SQLite?
- 28. Disallow Matlab per prendere fuoco automaticamente
- 29. Disallow broadcast nel metodo convertAndSendToUser in SimpMessagingTemplate
- 30. Consenti solo numeri nella casella di testo in GWT?