1
votes

Im new to powerbuilder:

I want to validate email text field after its done typing on it and go to another text field.

so I put my codes at "itemchanged event of the datawindow"

heres my code:

choose case dwo.name
case 'email'
  if data <> '' then
    if match(data,'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z][a-zA-Z][a-zA-Z]*[a-zA-Z]*$') then
    else
      This.SetText('')
      MessageBox('Error', 'This is an invalid email')
    end if
  end if
end choose

What happens here is that when i type for example 'asd'.

So it is an invalid email becoz it does not pass the match

Its actually clears the email field but after the messagebox fired and I clicked OK.

'asd' value returns on email field which I had cleared.

I think that's weird?

Where event should I put my validation and how to code this? Or there is a code for this in itemchanged so that I can clear the email field?

Anybody who can help me?

1
OFFTOPIC: Is not this more correct to check email? match(mail,'^[a-zA-Z0-9][a-zA-Z0-9\-_\.]*\@[a-zA-Z0-9][a-zA-Z0-9\-_\.]*\.[a-zA-Z0-9\-_\.]+[a-zA-Z0-9]+$' )Eduardo G.
ow thanks for the correction. like [email protected]Jomar Gregorio

1 Answers

1
votes

Luckily I fixed it by doing this in dw itemchanged event.

choose case dwo.name
case 'email'
  if data <> '' then
    if match(data,'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z][a-zA-Z][a-zA-Z]*[a-zA-Z]*$') then
    else
      MessageBox('Error', 'This is an invalid email')
      Return 1 //means does not accept the value and triggers itemerror.
    end if
  end if
end choose

By adding return 1 it gives another message box.

Data Window Error: This 'asd' does not pass the validation

To override this default error message.

Add some chunk of codes at dw itemerror event

CHOOSE CASE dwo.name   
  CASE 'email'  
    This.setText('') //if you just want to validate just remove this.
    Return 2 //means does not accept the value and does not let losefocus
END CHOOSE 

. :D