View Full Version : Vbscript error checking
scriptblur
10-31-2002, 06:07 AM
Hi guys....Please help
How can i checking all the ASCII codes (~,`,!,@,#.... etc)
using VBscript only?
like checking for password... user cannot enter such codes for their
password.... all ideas are welcome,..... thank....
Mhtml
10-31-2002, 06:18 AM
Where's Whammy when you need him eh?:)
This sounds to me like a regex job but since I'm not so good at regex I will show yo another way!
I was given this to validate my linkPRO asp script with it works, I used it for cuss words and XXX words.
dim txtURL, arrBad
txtURL = request.form("FormName")
arrBad = array("!","@","#","$","%","^","&","*","(",")","+","|","\","/","-","=" 'put bad text here
for i = 0 to ubound(arrBad)
If InStr(txtURL , arrBad(i)) then
Response.Redirect("illegal.asp")
exit for
End If
next
scriptblur
10-31-2002, 06:37 AM
thank Mhtml,
but your method cant fix into mine.... actually i am doing password changing... the codes i use to check are as below:
if the user any unwanted codes (@#$....), it will prompt them a error message box.... is that any way to implement this function into my codes below???
Sorry... the codes are very messy.....
<script language="vbscript">
sub button_OnClick
if (form.currentpassword.value <> "" or form.newpassword.value <> "" or form.changepassword.value <> "") then
if (form.currentpassword.value <> "" or form.newpassword.value <> "") then
if (form.currentpassword.value <> "" or form.changepassword.value <> "") then
if (form.newpassword.value <> "" or form.changepassword.value <> "") then
if form.currentpassword.value <> "" then
if form.newpassword.value <> "" then
if form.changepassword.value <> "" then
if form.currentpassword.value = "<%=Session("Pass")%>" then
if Len(form.newpassword.value) = "6" then
if Len(form.changepassword.value) = "6" then
if form.newpassword.value = form.changepassword.value then
form.submit
else
alert "The new and confirm passwords must be the same!"
end if
else
alert "Please enter new 6 digit passwords for confirmation"
form.changepassword.focus
end if
else
alert "Please enter new 6 digit passwords"
form.newpassword.focus
end if
else
alert "Please enter the correct Password"
form.currentpassword.focus
if Len(form.currentpassword.value) <> "6" then
alert "Please enter only 6 digit passwords"
form.currentpassword.focus
end if
end if
else
alert "Please confirm the new password (6 digit password)"
form.changepassword.focus
end if
else
alert "Please enter the new password (6 digit password)"
form.newpassword.focus
end if
else
alert "Please enter your current password (6 digit password)"
form.currentpassword.focus
end if
else
alert "Please enter the other two fields (6 digit password)"
end if
else
alert "Please enter the other two fields (6 digit password)"
end if
else
alert "Please enter the other two fields (6 digit password)"
end if
else
alert "Please enter all the fields (6 digit password)"
end if
end sub
Mhtml
10-31-2002, 07:43 AM
Not sure, I think it may be best left to a more experienced person like roy sinclair or glenngv
scriptblur
10-31-2002, 07:53 AM
its ok.. thank anyway....
so can roy sinclair or glenngv or anyone can help me with this problem
dominicall
10-31-2002, 10:25 AM
Hi scriptblur
Looking at your code, it looks like your doing this check with VBscript and mixing up client side and server side... which you can't do. I'm happy to stand corrected on this one if anyone thinks differently.
To do all the initial checks, making sure that the new password fields match, the lengths are OK, they're not blank, etc, etc... the best solution is to use a client side check with Javascript. This will be supported by the vast majority of all browsers. I've attached my form checking script for you to have a look at (credits to Chris Nott at www.dithered.com whose script I've used and adapted).
(Note, my script won't do all your checks but will give you an idea of what to do. I'd thoroughly recommend learning and using regular expressions - a very powerful tool.)
If the javascript check fails you can pop up an alert box highlighting all the errors and cancel the form submission.
To call the function, do the following...
Step one - inlcude the following in the HEAD of your document
<script language="JavaScript" src="/includes/checkform.js"></script>
Step two - call the function using the onSubmit event of the form as follows:
<form name="name" method="post" action="action.asp" onSubmit="return ChkForm(this);">
If the form passes all the javascript checks then it will be submitted and then you can do the server side check to ensure that the original password entered matches the session variable.
If it doesn't, you can't then use an alert box since this is a client side function. The easiest way to do it is to run the check and create an error querystring if the check fails and redirect back to the form, as below:
Dim ErrorQS
ErrorQS = NULL
If Request.Form("originalpassword") <> Session("Pass") Then
ErrorQS = "&error=nomatch"
End If
If ((ErrorQS <> "") OR (Not IsNull(ErrorQS))) Then
Response.Redirect "formpage.asp" & ErrorQS
Else
'----- process the form script
End If
I've been a bit verbose with the code above to demonstrate the logic - if you're only doing 1 check then you can just do the redirect from the first check. Doing the way I have above, you can do multiple check with an If...Then...Else and return different error values.
All you need to do on the form page is check the URL for the error querystring and show an error message above the form (well that's how I do it anyway). For example:
Dim ErrorMsg
ErrorMsg = NULL
If Request.QueryString("error") = "nomatch" Then
ErrorMsg = "The original password is incorrect - please try again"
Response.Write ErrorMsg
End If
<form....... now show the form
If you have 5 checks for different things on the server side then you can use If...Then...Else in the code above to create a different error message for each different error passed back in the querystring.
I hope the above helps.
Dominic :D
Again, respect to Chris Nott at dithered for his cool form checking script.
scriptblur
11-01-2002, 05:08 AM
hi guys... thank a lot for all the helps...
i had solve all the problem... using javascript and vbscript to check for all the errors...
i use javascript to block the shift key and spacing problem and vbscript to deny blank fields, inappropriate length and invalid password..... thank again for everyone helps.................
whammy
11-02-2002, 12:34 AM
Hey,
You might want to look at the regex script I posted awhile back.:
http://www.solidscripts.com/regex.txt
If you only want to allow A-Z, a-z, 0-9, and _ in a username or password, you can simply modify my:
ExtractAlphaNumeric()
function like so:
<%
username = "Bob#9"
usernameerror = False
Function CompareAlphaNumeric(ByVal str) ''''''''''''''''
If IsNull(str) Then str = ""
Dim canRegEx
Set canRegEx = New RegExp
canRegEx.Pattern = "\W"
canRegEx.Global = True
CompareAlphaNumeric = canRegEx.Replace(str,"")
End Function '''''''''''''''''''''''''''''''''''''
If CompareAlphaNumeric(username) <> username Then usernameerror = True
Response.Write(username & " is not a valid username: " & usernameerror)
%>
Example: http://www.solidscripts.com/testreg.asp
P.S. Note to Mhtml, glenngv, and Roy Sinclair... I noticed this was a great opportunity to test "ByVal" and show it in an example. Obviously "ByRef" is the default and doesn't need to be specified in ASP/VBScript functions... that was good practice, LOL.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.