---
title: "extjs4重写验证textfidld不能输入空格"
author: "chen2991101"
date: "2013-04-17T21:25:17Z"
tags: ["Extjs4", "空格"]
categories: ["Web前端"]
slug: "extjs4-textfidld"
draft: false
lastmod: "2013-04-18T10:50:42Z"
id: 1849147
url: "https://chen2991101.iteye.com/blog/1849147"
---

在extjs4中的表单验证里面虽然能够通过allowBlank : false配置控件不能为空，但是如果在控件里面输入空格的话就很轻松的骗过了验证，这显然是不合理的，我想到的解决办法是通过重写TextField控件的验证器再加一个判断空格的验证：

```
Ext.apply(Ext.form.TextField.prototype, {    
            validator : function(text) {    
                if (this.allowBlank == false && Ext.util.Format.trim(text).length == 0) {    
                    if (text.length > 0) {    
                        return '非法字符';    
                    }    
                } else    
                    return true;    
            }    
        });   
```