Forum Moderators: open

Message Too Old, No Replies

Stripping a variable down for confirmation script

How do I strip a variable down for confirmation script?

         

Jeremy_H

3:14 pm on Sep 8, 2005 (gmt 0)

10+ Year Member



I'm trying to write a script that confirms an entered value and either continues or else prompts the user with an error.

Characters 1-2 must be "e3", "c5" or "d7",
Characters 3-4 must be between 52 and 71,
Character 5 must be 8.

It would seem pretty simple to write, but I don't know how to strip the variable down to the the first two, middle, or last characters to see what they equal.

Any ideas? Thanks!

Bernard Marx

3:21 pm on Sep 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look up the string method, substring.

[google.se...]

var str = "what is this";

alert(str.substring(1,3)) // 'ha'

Stooshie

3:51 pm on Sep 8, 2005 (gmt 0)

10+ Year Member



You might be better using regular expressions.

[regexlib.com ]

Bernard Marx

9:28 pm on Sep 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



/^(e3¦c5¦d7)(5[2-7]¦6[0-9]¦7[01])8$/

#2 required a bit of thinking. I generally try to avoid that.


/*-------------------------------------------
1) Characters 1-2 must be "e3", "c5" or "d7",
2) Characters 3-4 must be between 52 and 71,
3) Character 5 must be 8.
---------------------------------------------*/

strings =
[
'e3568',/* T */
'c5568', /* T */
'd8568', /* F:1 */
'd7898', /* F:2 */
'c5719' /* F:3*/
]

reg = /^(e3¦c5¦d7)(5[2-7]¦6[0-9]¦7[01])8$/g;

for( k=-1; s=strings[++k];)
alert( reg.test(s) );

Jeremy_H

1:12 am on Sep 9, 2005 (gmt 0)

10+ Year Member



Thank you everyone for all your help!

With the link provided, I was able to write this function:

function reprint(){get_game();if(game_num.slice(4,5)=="8" && game_num.slice(2,4)>"51" && game_num.slice(2,4)<"72") {if(game_num.slice(0,2)=="e3" ¦¦ game_num.slice(0,2)=="c5" ¦¦ game_num.slice(0,2)=="d7") {window.location='pdf/'+game_num+'.pdf';}else{alert("Bad Game Number!");}}else{alert("Bad Game Number!");}}

I know it's probably not the most efficient piece of script out there, but it's a starting point, and it works.

Stooshie, Bernard, I know regular expressions is the way to go, but they are so difficult the figure out, Bernard, thanks for working out the problem, that's awesome!

In the script above, to use the regular expression, would it be something like this:

function reprint(){get_game();if(game_num==/^(e3¦c5¦d7)(5[2-7]¦6[0-9]¦7[01])8$/){window.location='pdf/'+game_num+'.pdf';}else{alert("Bad Game Number!");}}

Thanks again for your feedback!

Bernard Marx

2:44 am on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops!

/^(e3¦c5¦d7)(5[2-9]¦6[0-9]¦7[01])8$/

Jeremy_H

3:19 pm on Sep 9, 2005 (gmt 0)

10+ Year Member



OK, tinkering with it some more I was able to get:

function reprint(){get_game();var filter = /^(e3¦c5¦d7)(5[2-9]¦6[0-9]¦7[01])8$/;if (filter.test(game_num)) {window.location='pdf/'+game_num+'.pdf';};else {alert("Bad Game Number!");}}

It functions, but it invalidates all numbers. Am I implementing regular expression wrong?

Thanks.

Bernard Marx

3:26 pm on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) (In case you didn't know) WebmasterWorld's CGI script turns all corrupts vertical pipe characters (¦). You will have to change them into unbroken ones yourself.

2) The regexp has a $, so the code must be exactly 5 chars long. If characters after the '8' are allowed, just remove the $

Jeremy_H

3:54 am on Sep 15, 2005 (gmt 0)

10+ Year Member



Thank you very much!

I didn't realize the site made the pipes broken. :( Once I learned that, the script worked great!

Thanks again!