0

I've recently been trying to validate a user input so that only letters from the alphabet are accepted, how would I do this? I know how to validate user input for most things, but this one line of code for letters is really troubling me.

Mark
  • 2,380
  • 11
  • 29
  • 49
  • You will want to add some code to show what you have tried so far. Otherwise, this question is very likely to be closed or even deleted. – Mark Oct 17 '14 at 15:36

1 Answers1

0

You can check the contents of a field with this function:

function validate theString
  return matchText(theString,"^[a-zA-Z]+$")
end validate

^[a-zA-Z]+$ is a regular expression. ^indicates the beginning of a string, the brackets equal one char and the expression inside the bracket determine a set of characters. The + means that all following characters have to be equal to the preceding (set of) character(s). The $ indicates the end of the string. In other words, according to this expression, all characters must be of the set a up to and including z or A up to and including Z.

matchText() is a LiveCode function, which checks if the string in the first parameter matches the regular expression in the second parameter. Put the validate() function somewhere at card or stack level and call it from a field in a rawKeyUp handler:

on rawKeyUp
  if not validate(the text of me) then
    beep
    answer "Sorry, that's wrong"
  end if
end rawKeyUp

You could also check beforehand:

on keyDown theKey
  if validate(theKey) then
    pass keyDown
  end if
end keyDown

This method is slightly verbose. You could also put the matchText function in a keyDown handler of your field.

Mark
  • 2,380
  • 11
  • 29
  • 49