Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to make some code into a function.. (Basic)

Status
Not open for further replies.

DSect

Programmer
Sep 3, 2001
191
US
Hello..

I have some code used for server-side form input validation.


This loops through each char in the string and checks to see if it's a valid character (based on ASCII values)

Code:
    DIM unamemText, unamemOKstr, unamemChr, unamemASCchr, unamemRet
    DIM unamei
    DIM unamemErrMsg, unamemBlankRow
    unamemBlankRow = &quot;<TR><TD> </TD></TR>&quot;
    unamemText = uname
    unamemOKstr = &quot;_.-@&quot;
    unamemret = True
    FOR unamei = 1 TO Len(unamemText)
   	    unamemChr = Mid(unamemText, unamei, 1)
          unamemASCchr = ASC(unamemChr)
          IF NOT ((unamemASCChr >=65 AND unamemASCChr <=90)  _
		  OR (unamemASCchr >=97 AND unamemASCchr <=122) _ 
		  OR (unamemASCchr >=48 AND unamemASCChr <=57)) Then
                IF Instr(1, unamemOKstr, unamemChr)= 0 Then
                   unamemRet= False
                    Exit For
                 END IF
          END IF
    NEXT
    IF unamemRet = False Then
		errExists = errExists + 1
		errUname = &quot;<B>Username</B> contains invalid characters.<BR>&quot; & _
		&quot;Valid Characters are &quot; & unamemOkstr & &quot;<BR>&quot;
    END IF

I would like to make this into a function or sub (whatever is best).

I imagine that there is a way to make the &quot;invalid input checker&quot; into a function. I'd love to be able to use this validation thing by doing something like:

FunctionName(uname)
Where FunctionName is the name of my function and UNAME is the variable that it is checking for validity.

I am learning about functions, but I cannot seem to figure out WHERE or WHAT to put in the argument list that follows the function name when it's used or called.

Example:
In the char validation code, the only place I refrence the variable to be evaluated is the line:
unamemText = uname

Question:
How do I make the above into a function and what do I put in the code or arglist to make it so I can simply do a FUNCTION(variable.to.be.validated) and the variable that I put in the arglist would be tested using my code.

(plz ignore the error message stuff. I'll take care of that once I learn how to make and use the function properly)

Plz let me know if I need to clarify, because I bet it's a simple question that I am making sound really really complicated.

-* D *-

 
I put some alerts in there to test but it works. Is this what you are looking to do??
<html>
<head>
<script language=&quot;Vbscript&quot;>
Function Validate()
DIM unamemText, unamemOKstr, unamemChr, unamemASCchr, unamemRet
DIM unamei
DIM unamemErrMsg, unamemBlankRow
unamemBlankRow = &quot;<TR><TD> </TD></TR>&quot;
unamemText = form1.uname.value
alert unamemText
unamemOKstr = &quot;_.-@&quot;
alert unamemOKstr
unamemret = True
FOR unamei = 1 TO Len(unamemText)
unamemChr = Mid(unamemText, unamei, 1)
unamemASCchr = ASC(unamemChr)
IF NOT ((unamemASCChr >=65 AND unamemASCChr <=90) _
OR (unamemASCchr >=97 AND unamemASCchr <=122) _
OR (unamemASCchr >=48 AND unamemASCChr <=57)) Then
IF Instr(1, unamemOKstr, unamemChr)= 0 Then
unamemRet= False
Exit For
END IF
END IF
NEXT
IF unamemRet = False Then
errExists = errExists + 1
document.write &quot;<B>Username</B> contains invalid characters.<BR>&quot; & _
&quot;Valid Characters are &quot; & unamemOkstr & &quot;<BR>&quot;
END IF


end Function
</script>
</head>
<body>
<form name=&quot;form1&quot;>
<input type=&quot;text&quot; name=&quot;uname&quot;>
<input type=&quot;button&quot; value=&quot;validate&quot; onClick=&quot;Validate()&quot;>
</form>
</body>
</html> I may not get it the 1st or 2nd time,
but how sweet that 15th time can be.
 
Kinda.

I have 13 variables that come from form fields and many have different validation requirements, except char validation - they all need to pass this test.

I was hoping to be able to make that character validation routine into something that I can re-use and call easily.

I also wanted to have the function so it's not hard-coded with the variable that it is checking.. That way, if you want to validate a different variable, then you could simply just say:

FunctionName(uname) to validate the UNAME variable.

or

FunctionName(pword) to validate the PWORD variable

See what I mean? I'm not sure what it's called (function or sub) but I think it's doable in VBscript (maybe it's not).

Either way. With a function, the variables are only valid for the function so I can reuse the validation routine without having to make uniquie variables for it's operations (which is like kidnergarten coding).

That's why I'm trying to get it into (what I call) a &quot;Dynamic Function&quot;.

At worst, I can use what I have now and make a seperate function for each variable to be evaluated, but that seems wacky to me, considering the only things that makes the function different is what variable it evals and the text of the error message - at least for character validation, as all var's need to pass the character validation.

Am I like in the totally wrong direction? I am very newbie, so I am thinking like a beginner.

thanks 4 the help, though!!
 
Just loop through all the form elements in the function instead of declaring them seperatly I may not get it the 1st or 2nd time,
but how sweet that 15th time can be.
 
Sounds like a job for: FOR..EACH..

I'll work on doing it that way.

Thanks for the help in the meantime!

+K to ya!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top