2013-03-06 23 views
0

我有密碼字段。它檢查是否有最少8個字符和屬性設置爲必需。但是,我不希望用戶在文本字段中輸入任何空格。我怎樣才能做到這一點 ?Flex:如何驗證文本字段中的空格?

這是我的驗證碼在我的聲明標籤:我的MXML

<mx:StringValidator id="userPasswordValidator"       
         property="text"              
         required="true"       
         minLength="8"       
         tooShortError="password must at least be 8 characters" 
         source="{userPassword_field}"/> 

我的文本字段:

<s:TextInput id="userPassword_field" x="73" y="48" width="109" height="21" displayAsPassword="true"/> 

請有人可以告訴我怎麼可以驗證在文本字段空間?

謝謝:)

回答

1

您可以簡單地停止用戶在文本輸入中輸入空格。只允許用戶在文本輸入中添加允許的字符。

<s:TextInput id="userPassword_field" x="73" y="48" width="109" height="21" displayAsPassword="true" restrict="A-Z\a-z\0-9"/> 

使用'restrict =「A-Z \ a-z \ 0-9」'來限制用戶。

+0

感謝這有助於:) – user2017147 2013-03-07 07:27:42

+0

如果我想允許像 - ,*,\這樣的字符,但只是不允許空格。什麼是正則表達式?目前它只允許使用字母和數字。 – user2017147 2013-03-07 07:33:27

+0

你可以從 (http://ryanswanson.com/regexp/)獲得幫助 這是一個正則表達式的好工具。並將maxChar屬性添加到textInput。 restrict =「A-Z \ a-z \ 0-9 * - 」 – 2013-03-07 09:12:08

0

簡單地檢查字段的內容與替換空格的內容。如果它們不相同,則告訴用戶。

e.g

<Input type="password" onchange="return noSpaces(this)" /> 
function noSpaces(myInputElem) 
{ 
    var returnVal=true; 
    if(myInputElem.value!=myInputElem.value.replace(" ","")) 
    { 
    alert("Spaces not allowed"); 
    returnVal=false; 
    } 
    return returnVal; 
} 

我沒有測試過這一點,但多數民衆贊成這個想法。

+0

嗨,謝謝。我會試一試,讓你知道。 – user2017147 2013-03-07 08:10:23

0

設置限制= 「^」 將允許除空間的一切。