Home > OS >  How to validate a field only contains alphanumerics, spaces, dashes & always contains at least one a
How to validate a field only contains alphanumerics, spaces, dashes & always contains at least one a

Time:06-03

I have an input field that I need to add validation to in c#. The field should only allow alphanumerics, spaces, dashes, and must contain at least one alpha or numeric character. I have the layout of the function, but I am not sure how to formulate the conditions for the if statement.

//function call
 ValidateSchoolIDField(request.SchoolID, "School ID", validationErrors);

//function
private static void ValidateSchoolIDField(string value, string fieldname, List<ValidationError> validationErrors)
{
     if (request.SchoolID != alphanumeric/space/dash) 
        validationErrors.Add(new ValidationError("ivalid input for "   fieldName)):
} 

CodePudding user response:

Please try below Regex

Regex.IsMatch(inputString,"[a-zA-Z0-9\s-_]");

  • Related