开发者

action script 3.0 a string variable that should have only number

In action script var x:String="123abc" I have to check any character, for that string开发者_StackOverflow.

i.e. here "abc" is that string so I give an alert that this string should contain only numbers.

How can I do that?


Do you mean to say that you would like to dispatch an alert if a string contains letters

      var testVar:String = '123abc';
      var pattern:RegExp = /[a-zA-Z]/g;

      if( testVar.search(pattern) == -1 )
      {
           //all good there's no letters in here
      }
      else
      {
         //Alert, alert, letter detected!
      }

the "pattern" variable is a RegularExpression that's adaptable. Here I'm only checking for letters... If you need more control, get more info about RegularExpressions or come back here with the specific filter you'd like to implement.


I think you are looking for Regular Expression support in AS3.


If the user is inputting text via a TextField then you can set the restrict property to limit the characters that can be entered into the textfield:

textFieldInstance.restrict = "0-9";

TextField.restrict documentation:
http://livedocs.adobe.com/flex/3/langref/flash/text/TextField.html#restrict

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜