how do I ensure numeric value is passed to textbox?

samflex
11-16-2006, 09:00 AM
hi all,

I have a dropdown menu that gets populated with data from the db.

I have also created an additional input textbox so that if the option the user is looking for is not found in the dropdown menu, the user can manually enter the value into the input textbox.

This works great.

Here is the code that:


<tr>
<td><p align=right>
Agency:<font color="#ff0000">*</font></td>
<td>
<select name="agency">
<option value="">--select an agency--</option>

<%

sql = "SELECT agency_id, agencyName FROM EWI_Agency"

set rs = dbConn.Execute(sql)

While not rs.EOF

%>

<OPTION value="<%=rs(1)%>"><%=rs(1)%></OPTION>

<%

rs.MoveNext

wend

rs.close

set rs=nothing

%>

</select>
<INPUT Name="newAgency" Value=""><br>
<img name=agencyerror src="images/blankimage.gif" width=350 height=10 border=0></td>
</tr>


The problem I have so far is that if the option that is selected from the dropdown menu returns a integer value (agency_ID). However, if this data is not found in the dropdown menu and is entered into the textbox, it returns a text value.

As a result, when attempting to insert it into the main table, I get "error converting value to datat type int"

How do I either ensure that I am entering an integer value into the textbox as it is the case with dropdown OR somewhow convert the text into an integer before inserting into the main table?

Any ideas would be greatly appreciated.

Here is the code that gives the error:


agencyname = request.form("agency")
if agencyname = "" Then
agencyname = request.form("newagency")<-- This gives error because it is not an integer value
else
agencyname = request.form("agency")
end if


Thanks very much in advance

wayneph
11-16-2006, 11:34 AM
been a while since I've used classic ASP, but I think the IsNumeric() function exists and does what you're looking for.

you could also use a simple Regular Expression to see if your data is numeric.

alp0001
11-16-2006, 11:45 AM
1) For the textbox, you can change it so that only numeric data can be entered.

2) Based on #1 being included, also do a server side validation check. Yes, it may be more work, but it covers malicious/(Javascript turned off) users as well. Now, should the check fail, give an appropriate error back to the user stating that the value entered can only be numeric.

3) If the validation went ok, you can use the CInt function to treat the value as an integer before inserting it into the DB.

And Wayne gave you an idea as to what to use for #2.

samflex
11-16-2006, 12:23 PM
Thanks to Wayne and alp0001.

Ok, let me show precisely what I am talking about.

I ran the view and clicked on "View Source"

here is a small example of the kind of data I got from dropdown:


Agency:<font color="#ff0000">*</font></td>
<td>
<select name="agency">
<option value="">--select an agency--</option>



<OPTION value="1">ALPH</OPTION>



<OPTION value="2">APD</OPTION>



<OPTION value="3">Co SO</OPTION>



<OPTION value="4">CPD</OPTION>



<OPTION value="5">DL</OPTION>



<OPTION value="6">FPD</OPTION>



<OPTION value="7">FC</OPTION>



<OPTION value="8">SO</OPTION>



<OPTION value="9">FD</OPTION>

As you can see, the value saved on the db is the numeric value that starts from 1 till 9. These values are identity keys on the database. A user would not know what the numbers are.

So, since they don't know what the numeric equivalences are, they would enter for instance <OPTION value="8">SO</OPTION>
SO instead of the number 8.

You see that not even the IsNumeric() function can't convert So to 8.

wayneph
11-16-2006, 12:59 PM
what you'll need in that case is a lookup function. given SO, call the database and lookup the key value. then you store that value.

if you're using SQL Server, or anything else that has stored procedures, you can pass in the 'SO' value and do the lookup directly on the server. It will be a little more efficient than making round trips to the server.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum