• Advertisement
Sign in to follow this  

ASP Help

This topic is 4207 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys. It's been a long time since I've last posted. I was wondering if anyone here can help me with an ASP problem? Basically I am making a form which accepts a bunch of numbers to be inputed to the database. How can I make a validation check to make sure every field on the textbox is an integer, or even better, if there is a way to make sure they can't type anything other than "0-9" on the textbox? many thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Add this little snippet:

<asp:RangeValidator
id="NumberValidator"
ErrorMessage="Must be number between 0 and 9"
ControlToValidate="txtNumberEntry"
MaximumValue="9"
MinimumValue="0"
Type="Integer"
Display="Dynamic"
Runat="Server" />

In this case, the number being validated has been entered into a text box named txtNumberEntry (ControlToValidate) max and min values are entered in the relevant places, type is set to integer so that it tests against the integer values as opposed to string. Now when the submit button is clicked the values in the box is validated. Just add the range validator to each control to be validated and change the ControlToValidate parameter and values parameters accordingly.

Hope that helps

Share this post


Link to post
Share on other sites
Is this ASP, or ASP.NET? I believe Medhatter's solution is for ASP.NET.

If you're using ASP, just insert a little javascript into the page code to be run when the user hits 'submit' on the form. The javascript can do all the checks you want, and then return true or false to control whether the form is OK to be sent to the server.

That wouldn't stop a hacker, of course... so, on the server, just scan the input strings for invalid characters (non-numbers).

Share this post


Link to post
Share on other sites
Sorry, yeah, my solution's ASP.Net, started out with .Net so when I see ASP that's what I take it to be. Assumption's the mother of all mistakes and all that...

Share this post


Link to post
Share on other sites
Quote:
Original post by superpig
Is this ASP, or ASP.NET? I believe Medhatter's solution is for ASP.NET.

If you're using ASP, just insert a little javascript into the page code to be run when the user hits 'submit' on the form. The javascript can do all the checks you want, and then return true or false to control whether the form is OK to be sent to the server.

That wouldn't stop a hacker, of course... so, on the server, just scan the input strings for invalid characters (non-numbers).


sorry for the late reply, but which Java code can I use for ASP? is there an example of this?

thank you.

Share this post


Link to post
Share on other sites
In your case, the best way would be to do it in pure javascript, that will check it client side only. I don't have any example or any idea how to do this exactly. But I searched and found a function http://www.codetoad.com/javascript/isnumeric.asp. You can use that function in combination like this for example:

<script>
function check() {
var field=document.formname.fieldname.text;

if (!IsNumeric(field) && !field.length==1) {
alert("You most enter a number: ");
return false;
}

return true;
}
</script>

<form action="?" method="POST" onSubmit="check();">
....

This will make the user forced to enter a number between 0-9 with a maxlength of 1 character, haven't tested it though.

Share this post


Link to post
Share on other sites
I use this function

Function IsACLng(sNum, iNum)

IsACLng = False
If IsNumeric(sNum) And Len(sNum)>0 Then
iNum = CLng(sNum)
IsACLng = True
End If
End Function

like this

sNumber = Trim(Request.Form.Item("MyNumber"))
If IsAClng(sNumber, iNumber) Then
'do somthing with iNumber
Else
iNumber = 0
End If

Share this post


Link to post
Share on other sites
thanks alot guys. another question.

my asp's date is arranged as DD/MM/YYYY
but my SQL server date is arranged as MM/DD/YYYY

how do I convert from DD/MM/YYYY to MM/DD/YYYY? thanks

Share this post


Link to post
Share on other sites
Aren't I generous?


sQuery = "SELECT * FROM Event"
sQuery = sQuery & " WHERE Event.Happened"
sQuery = sQuery & " >= { d '" & SQLDate(dStart) & "' }"
sQuery = sQuery & " AND Event.Happened"
sQuery = sQuery & " <= { d '" & SQLDate(dEnd) & "' }"


Function SQLDate(dContributionDate)
Dim s

s = Year(dContributionDate) & "-"
If Month(dContributionDate)<10 Then
s=s & "0"
End If
s=s & Month(dContributionDate) & "-"
If Day(dContributionDate)<10 Then
s=s & "0"
End If
s=s & Day(dContributionDate)
SQLDate=s
End Function

Function IsADate(sDay, sMonth, sYear, ByRef dDate)
Dim dTemp
Dim iDay
Dim iMonth
Dim iYear

IsADate = False
If IsACLng(sDay, iDay) _
And IsACLng(sMonth, iMonth) _
And IsACLng(sYear, iYear) Then
dTemp=#1/1/2000#
dTemp = DateAdd("YYYY", iYear-2000, dTemp)
dTemp = DateAdd("M", iMonth-1, dTemp)
dTemp = DateAdd("D", iDay-1, dTemp)
If Year(dTemp) = iYear _
And Month(dTemp) = iMonth _
And Day(dTemp) = iDay Then
dDate = dTemp
IsADate = True
End If
End If
End Function

Function IsAStringDate(sDate, dDate)
Dim sDay, sMonth, sYear

sYear = Mid(sDate ,7, 4)
sMonth = Mid(sDate, 4, 2)
sDay = Left(sDate, 2)
IsAStringDate = IsADate(sDay, sMonth, sYear, dDate)
End Function

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement