username@email.com
2024-10-29 3f91a6737fc06b45461ce11eae5660cbbf766f7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using NPOI.OpenXmlFormats.Dml;
 
namespace CoreCms.Net.Utility.Helper
{
    public static class FormHelper
    {
 
        /// <summary>
        /// 验证字段类型及提交的值是否对应
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static bool ValidateField(string typeName, object thisValue)
        {
            var bl = false;
            var valueType = thisValue.GetType();
            if (typeName == GlobalEnumVars.FormValidationTypes.字符串.ToString())
            {
                return valueType == typeof(string);
            }
            else if (typeName == GlobalEnumVars.FormValidationTypes.数字.ToString())
            {
                return thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out _);
            }
            else if (typeName == GlobalEnumVars.FormValidationTypes.整数.ToString())
            {
                return thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out _);
            }
            else if (typeName == GlobalEnumVars.FormValidationTypes.价格.ToString())
            {
                return thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out _);
            }
            else if (typeName == GlobalEnumVars.FormValidationTypes.邮箱.ToString())
            {
                if (valueType == typeof(string) && !string.IsNullOrEmpty(thisValue.ToString()))
                {
                    return Regex.IsMatch(thisValue.ToString() ?? string.Empty, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");
                }
                else
                {
                    return false;
                }
            }
            else if (typeName == GlobalEnumVars.FormValidationTypes.手机号.ToString())
            {
                if (valueType == typeof(string) && !string.IsNullOrEmpty(thisValue.ToString()))
                {
                    return CommonHelper.IsMobile(thisValue.ToString());
                }
                else
                {
                    return false;
                }
            }
            else if (typeName == GlobalEnumVars.FormValidationTypes.多数据.ToString())
            {
                return valueType == typeof(Array);
            }
            else
            {
                bl = false;
            }
            return bl;
        }
 
 
    }
}