-
Edit box question
Hi, this may seem kinda dumb but I tried a lot of things and they didn't work so yeah. I'm working on an application where a user needs to enter their desired name into an Edit Box, but I don't want there to be spaces in their name. Is there any condition I can use to check if there are no spaces in an edit box?
-
Re: Edit box question
You could try the masked edit object. It might just be available for MMF 2 Pro - I can't remember.
Steve
-
Re: Edit box question
I tried using the Kernel object to trap On_Char messages, however it doesn't get as far as the Kernel object (Suggestion: Could the kernel object handle messaqes before the object?) and so you can't do that (You could see if it was a space and just discard it if it is. That is how you'd do it in C).
Anyway, the cheap solution is just to set the background red or something if you find "Find(Edittext$( "Edit Box" ), " ", 1)" is lower than or equal to zero (providing the text-box is non-empty)
-
Re: Edit box question
Learn how to use the String functions. You find them under the Special icon.
It is quite easy, anyway, I'll take you through it step by step.
When the user click the button to submit their name, start a fastloop. Name the loop "findWhitespace".
The fastloop need to be as long as the entered name. To get the length of the name, select Retrieve data from object, click on the System icon, and select Strings / Length of string.
Next make a new event with an onloop condition. The name of the loop must be the same as the loop you created in the other event.
You also need to test if the current character in the loop is equal to " ". To do that, make a compare two general values condition. As the first value, hit the Retrieve data from object button and select string / 'extract middle substring' from the Special icon.
You will have to input 3 values. The first one is the entered name. Retrieve it from the edit object. The second one is the character you test for, which should be the current loopIndex. Retrieve that from the Special icon: Fastloops / get loop index.
Set the last value to 1, since we test one character.
Compare this with " " and click OK. If this condition is true it means the name should not be allowed, so you will have to set a value to something so mmf knows that the button shall not perform an action.
I would simply make a string and write an errormessage if the name contains whitespace. Then test if the string have not gotten the errormessage.
Here's the code for you:
[color:#FF0000]* user clicks on the button[/color]
- start loop "findWhitespace"
- String1: set alterable string to "valid".
[color:#FF0000]* on loop findWhitespace"[/color]
+ Mid$(Edittext$( "Edit_name" ), LoopIndex("yourloop"), 1)
equals " "
- String1: set alterable string to "sorry, name must not contain whitespace"
[color:#FF0000]* user clicks on the button[/color]
+ String1: alterable string equals "valid"
- do your thing
-
Re: Edit box question
Thanks popcorn! I had some trouble at first but I found out I was starting the loop before setting the string to Valid(when the user clicks the button). Thanks, it works great now.
-
Re: Edit box question
You can just use the Find function rather than use a fastloop though...
-
Re: Edit box question
Or string manipulator and replace " " with ""
-
Re: Edit box question
Problem is with that is it will reset the cursor and I don't think there are methods for putting it back?
-
Re: Edit box question
I'm not sure I don't have MMF infront of me to test it. But I have used the string manipulator object and just stored the data without spaces rather than removing the spaces in runtime...
-
Re: Edit box question
Wouldn't it be the easiest to just disable / ignore the spacebar during typing?
-
Re: Edit box question
i'd just use popcorn's method, but it might be better if spaces are removed automatically after the entry is made. i wouldn't try any "realtime" whitespace prevention or removal
and yes, doing practically any sort of text manipulation on an edit box sends the caret to the beginning of the edit box (which i have found extremely annoying in cases)