Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC ASP.NET提交表单之前,如何在视图页面中输入数据后删除“我的输入”文本框中的空白?_C#_Jquery_Html_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 在MVC ASP.NET提交表单之前,如何在视图页面中输入数据后删除“我的输入”文本框中的空白?

C# 在MVC ASP.NET提交表单之前,如何在视图页面中输入数据后删除“我的输入”文本框中的空白?,c#,jquery,html,asp.net,asp.net-mvc,C#,Jquery,Html,Asp.net,Asp.net Mvc,如何在这个问题上修剪空白?我试过自定义活页夹。有人能帮我吗?我已经验证了输入字段。后来我注意到有人可以输入空白并提交表单。在提交文本之前,有人能帮我删除空白吗 //优惠券的型号 型号: public int CouponId { get; set; } [Required] public string CouponCode { get; set; } [Required] public int? Discount { get; set; } public List<CouponMod

如何在这个问题上修剪空白?我试过自定义活页夹。有人能帮我吗?我已经验证了输入字段。后来我注意到有人可以输入空白并提交表单。在提交文本之前,有人能帮我删除空白吗

//优惠券的型号
型号:

public int CouponId { get; set; }

[Required]
public string CouponCode { get; set; }

[Required]
public int? Discount { get; set; }

public List<CouponModel> GetList()
{
    List<CouponModel> couponList = new List<CouponModel>();
    couponList = db.Coupons.Select(m => new CouponModel
    {
        CouponId = m.CouponId,
        CouponCode = m.CouponCode,
        Discount = m.Discount
    }).ToList();
   //Trim White spaces in Coupon Text box
div class="row">
@if (ViewBag.Message == null)
{
    <div class="col-md-8">
        @using (Html.BeginForm())
        {
            <div class="input-group">
                                  <input id="CouponCode" name="CouponCode" type="text" class="form-control" placeholder="Coupon Code">
                <span class="input-group-btn">
                    <button id="btnApply" class="btn btn-default" type="submit">Apply Coupon</button>
                </span>
            </div>
        }

    </div>
}
<div class="col-md-4">
    @ViewBag.Message
</div>

为此,您可能需要某种基于正则表达式的验证。模式将匹配[^\s]+。(即,如果它包含任何非空白字符1次或多次,则有效。)您可以使用html5的验证属性来实现这一点。模式是您想要的,validationmessage可用于指定blur上显示的消息

它看起来是这样的:

<input id="CouponCode" name="CouponCode" type="text" class="form-control" placeholder="Coupon Code" pattern="[^\s]+" validationmessage="Coupon Code is a required field">
[RegularExpression("[^\s]+", ErrorMessage="Coupon Code is a required field")]

使用字符串时,请始终检查null、string.Empty和空格。您可以使用
string.IsNullOrEmpty(string)
检查null或Empty值,当您检查null、string.Empty和空格时,您可以使用
string.IsNullOrWhiteSpace(string)
。由于.Net Framework 4.0对字符串有一个
IsNullOrWhiteSpace()
方法,该方法将
IsNullOrEmpty()
方法推广到除空字符串外还包括其他空白

请试试这个:

public ActionResult Index(string CouponCode)
    {
        CouponCode= CouponCode.Trim();
        var cart =(CartModel)System.Web.HttpContext.Current.Session["cart"];
        if (string.IsNullOrWhiteSpace(CouponCode)) 
        {
        ModelState.AddModelError("", @"Your Error Message");//OR something you like.

        }

        if (ModelState.IsValid)
            {
           CouponModel model = new CouponModel();

        var coupon = model.GetList().FirstOrDefault(m => m.CouponCode == CouponCode);
        if (coupon != null)
        {
            cart.CouponApplied = true;
            cart.CouponId = coupon.CouponId;
            cart.CouponCode = coupon.CouponCode;
            cart.Price = cart.Price - cart.Price * 20 / 100;
            cart.Total = cart.Price * cart.Quantity;
            System.Web.HttpContext.Current.Session["cart"] = cart;              

            ViewBag.Message = "Coupon is applied Successfully";
        }
        else
        {
            ViewBag.Message = "Coupon not found or Expired!";
        }  

     }
       return View(cart);
    }

可以将模糊事件添加到输入字段

$("#CouponCode").blur(function() {
    $("#CouponCode").val($("#CouponCode").val().trim());
});
这样,您就不必担心发送带有空格的文本


这是一个正在工作的JSFIDLECouponCode=CouponCode.Trim().Replace(“,”);
这有助于

我可以在不申请优惠券的情况下继续签出页面。因此,我不明白为什么这里需要这个Reqex表达式?正则表达式将阻止某些人在文本框中只有一个空格的情况下提交表单。此外,您的模型已将CoupCode标记为[Required],因此不使用CoupCode提交表单是无效的。但问题是,即使客户键入带有空格的优惠券,也必须通过删除空格来应用此优惠券。谢谢您的回复。因此,您确实存在两个问题。你想阻止他们提交只有一个空格的优惠券代码,这就做到了。但您也希望从他们提交的内容中删除空格。这也可以通过正则表达式来实现。在控制器操作中:
Regex.Replace(CouponCode,[\s],”)这将从string.public int中删除所有空白字符?折扣{get;set;}public List GetList(){List couponList=new List();couponList=db.coups.Select(m=>new CouponModel{CouponId=m.CouponId,CouponCode=m.CouponCode,Discount=m.Discount}).ToList();Regex.Replace(CouponCode,[\s],”);return couponList;}表示字符串的转义序列。IsNullOrWhiteSpace(CouponCode)位是冗余的。您已经在上面对它进行了修剪,它不能再只是空白了。您可以删除CouponCode=CouponCode.Trim();严重性代码说明项目文件行抑制状态错误CS0161'CartController.Index(string)':并非所有代码路径都返回ValueController\CartController.cs 32 ActiveIt在提交值时未被修剪。在将此代码插入应用程序时,该值不变。
$("#CouponCode").blur(function() {
    $("#CouponCode").val($("#CouponCode").val().trim());
});