该字段必须是数字

时间:2015-05-26 03:35:07

标签: c# jquery datetime decimal globalization

我有这个字段:

数据库中的

@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) 为十进制(7,2)。

查看:

"The field must be a number"

如果我用逗号放一个值,MVC默认验证不接受,说:5,00, 55,00 or 555,00。 (我尝试使用正则表达式,但没办法)

例如:public DateTime date { get;set; }

还有:

 @Html.EditorFor(model => model.Date, 
                 new { htmlAttributes = new { @class = "form-control" } })

查看:

dd/mm/yyyy

MVC默认验证不接受mm/dd/yyyy格式的日期,仅在13/02/2015, 15/06/2013, 25/08/2012中。

例如:import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; public class WordCountWithCombiner extends Configured implements Tool{ @Override public int run(String[] args) throws Exception { Configuration conf = getConf(); Job job = new Job(conf, "MyJob"); job.setJarByClass(WordCount.class); job.setJobName("Word Count With Combiners"); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); return job.waitForCompletion(true) ? 0 : 1; } public static void main(String[] args) throws Exception { int exitCode = ToolRunner.run(new Configuration(), new WordCountWithCombiner(), args); System.exit(exitCode); } }

全球化还是什么?我该如何解决这个问题?

5 个答案:

答案 0 :(得分:12)

我找到的一个解决方案是覆盖jquery.validate.js

的验证功能
<script>

    $.validator.methods.range = function (value, element, param) {
        var globalizedValue = value.replace(",", ".");
        return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
    }

    $.validator.methods.number = function (value, element) {
        return this.optional(element) || /-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
    }
    //Date dd/MM/yyyy
    $.validator.methods.date = function (value, element) {
        var date = value.split("/");
        return this.optional(element) || !/Invalid|NaN/.test(new Date(date[2], date[1], date[0]).toString());
    }
</script>

答案 1 :(得分:1)

您必须使用[DataType(DataType.Currency)]标记您的媒体资源。

[DataType(DataType.Currency)]
public decimal Price { get;set;}

答案 2 :(得分:1)

你可能会在error with decimal in mvc3 - the value is not valid for field找到你的答案,它对我没有用,所以我用了这个临时的

<div class="col-md-10">
            @{ Html.EnableClientValidation(false); }
            @Html.EditorFor(model => model.DecimalValue, new { htmlAttributes = new { @class = "form-control" } })
            @{ Html.EnableClientValidation(true); }
            @Html.ValidationMessageFor(model => model.DecimalValue, "", new { @class = "text-danger" })
        </div>

我在这里找到ASP .NET MVC Disable Client Side Validation at Per-Field Level

答案 3 :(得分:0)

您可能希望使用[DisplayFormat]属性装饰您的字段,如下所示:

[DisplayFormat(DataFormatString = "{0:N}", ApplyFormatInEditMode = true)]
public decimal Price { get; set; }

答案 4 :(得分:0)

我有同样的问题,我曾经用全球化库(globalize.js)来解决它,但他们改变了它,因此它不包含本地化文件。它应该从cldr库中获取它们,但我还没弄清楚如何。