I need to validate a base64 string if it was encoded from a pdf file. The string must be:
- Starts with "JVBER" (to verify Pdf mime type)
- Matches "^[a-zA-Z0-9+/]*={0,3}$" and string length is multiple of 4 (to verify a valid base64 string)
Can anyone help me to combine those conditions into a regex?
Thanks.
public static bool HasPdfMimeType(string str)
{
if (!string.IsNullOrEmpty(str) && str.Length > 4)
{
return str.StartsWith("JVBER");
}
return false;
}
public static bool IsBase64string(string str)
{
if (string.IsNullOrEmpty(str))return false;
str = str.Trim();
return (str.Length % 4 == 0) && Regex.IsMatch(str, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None);
}