Page 1 of 1

"Is not" or "does not start with"?

Posted: Fri Sep 11, 2009 3:37 pm
by TodayIsTheDay
I have a text box and button...I'm trying to see if the beginning of a string in the text box starts with www. or http://, if not add them...

I came up with this:

Code: Select all

 if field "url" begins with "www." then put "http://"&field "url" into field "url"
It works but would it be better to say if field does not start with? I have searched for does not, etc but haven't found a way to do it...

I've also tried this:

Code: Select all

on mouseUp
   put wordoffset ("http://", field "url") into http
      
   put wordoffset ("www.", field "url") into httpwww
   
   if http <> 1 then put "http://"&field "url" into field "url"
   else 
   if httpwww <> 1 then put "http://www."&field "url" into field "url"
   
   put url field "url"  into field "main"
   end if
end mouseUp
I know there is a better way to do this...

Can someone point me in the right direction?

Thanks!

Posted: Fri Sep 11, 2009 4:05 pm
by BvG
I agree that the negative to "begins with" is missing. as a workaround, you can use "not" in the strangest places.

Code: Select all

 if not field "url" begins with "http://" then put "http://" before field "url"

Posted: Fri Sep 11, 2009 4:17 pm
by bn
for me it only works with brackets:

Code: Select all

on mouseUp
   if not (field "fURL" begins with "http") then put "http://" & field "fURL" into field "fURL"
end mouseUp
note that I replaced the name of the field "url" with "fURL". It is probably wise to stay away from reserved words for naming things in Rev.
regards
Bernd

Posted: Fri Sep 11, 2009 4:23 pm
by TodayIsTheDay
But my code needs to check to see if the string starts with www. if not add that, then it needs to see if it starts with http:// if not add http://www.

I'm just not getting the logic order or something I'm needing to do this wit...


Maybe an easier way to explain it is I have a search box I can enter a url into.
If the string doesn't start with http://www. then it won't work...

Posted: Fri Sep 11, 2009 4:38 pm
by BvG
more like this?

Code: Select all

if not field "url" begins with "www." then 
  put "www." & field "url" into field "url"
end if
if not field "url" begins with "http://" then 
  put "http://" & field "url" into field "url"
end if

Posted: Fri Sep 11, 2009 5:03 pm
by TodayIsTheDay
hmmm. something like that but I can't get that to work... Am I doing something wrong?

Posted: Fri Sep 11, 2009 5:19 pm
by TodayIsTheDay
I've also tried this:

Code: Select all

if not field "url" begins with "http://www." then put "http://www." & field "url" into field "url"
      put url field "url"  into field "main"
That doesn't work at all.

It also doesn't take into account if you've typed www.domain.com or http://domain.com into the box

Posted: Fri Sep 11, 2009 5:24 pm
by bn
if you put in the brackets Björnkes code works

Code: Select all

on mouseUp
   if not (field "furl" begins with "www.") then 
      put "www." & field "furl" into field "furl" 
   end if 
   if not (field "furl" begins with "http://") then 
      put "http://" & field "furl" into field "furl" 
   end if 
end mouseUp
if you want to catch http without www then you can try this

Code: Select all

on mouseUp
   -- test for double negative
   -- neither "http" nor "www" is in the beginning of the field
   if not (field "furl" begins with "http") and not (field "furl" begins with "www") then
      put "http://www." & field "furl" into field "furl"
   else
      -- test if field "furl" begins with "www"
      if  (field "furl" begins with "www") then put "http://" & field "furl" into field "furl"
      -- test for completeness 
      -- if address starts with "http://" but has no "www" in it (can probably omitted)
      if (field "furl" begins with "http://") and not (field "furl"   contains "www.") then put "www." after char 7 of field "furl"
   end if
end mouseUp
note that the field name I use is different from yours
regards
Bernd

Posted: Fri Sep 11, 2009 6:04 pm
by TodayIsTheDay
Thank you guys. The last code sample does exactly what I was looking for. It also taught me a couple of new things...

I'm sure I'll have more questions later...

Thanks again! :D

Posted: Fri Sep 11, 2009 6:29 pm
by user#606
I found that most useful, but any ideas to correct e-mail addresses?

the missing@ for example.

in my case, the WWW. bit works fine, even though the data is beyond correction by the user. The file is being saved by then, for instance.

Posted: Fri Sep 11, 2009 6:34 pm
by bn
could you give an example? To me it seems difficult to decide where to insert the @. If you only want to test whether the @ is in the address at all and then reject that mail-address that would be easy.
regards
Bernd

Posted: Fri Oct 02, 2009 2:52 am
by sturgis
Something like this for email validation should help. Its not really possible to correct an addy on the fly of course, just possible to check if it resembles the correct format.

And hi again everyone, I may be back momentarily.

Code: Select all

on mouseUp
   if not (field "furl" begins with "@") and (field "furl" contains "@") then
      -- This would indicate theres a good chance the email addy is valid. 
      --It doesn't START with @ but does contain one. 
      put "Email is valid format"
   else
      -- Fails the test, most likely is an invalid email, warn the user. 
    put "Email format is invalid"
  end if
end mouseUp
user#606 wrote:I found that most useful, but any ideas to correct e-mail addresses?

the missing@ for example.

in my case, the WWW. bit works fine, even though the data is beyond correction by the user. The file is being saved by then, for instance.

Posted: Fri Oct 02, 2009 9:48 am
by bn
Hi Sturgis,

good to see you back.

I hope it is going to be a long moment...

regards

Bernd