...

View Full Version : ASP to write Javascript



neocool00
08-12-2005, 04:52 PM
I have two database tables that are linked on a 12 digit alphanumeric field (I know that's bad, but I didn't design it and it has to be this way b/c that's how we get the data). Normally, I would just put a drop down for users to select which code that one to associate it, but in this case the lookup table consists of 12,000+ rows. What I am trying to do on the form is to expand my form validation as much as I can so that if the rules for code don't match then the form doesn't get submitted. Let me break down the value of the field:

digits 1 & 2 are numeric.
digit 3 is either 0 or N
digit 4 is a value from another lookup table
digit 5 & 6 are 0s (for now)

The problem I am having is that I want my regular expression to include knowledge of the fourth digit. Currently the values can be C, E, S or T and the administrators of the site have the ability to change those values. So ideally, I want to hit the database to see what values are valid for that field. I know that I could use asp to write out all of the JavaScript to the form and then for that part of the regular expression I could hit the database, but I was hoping for a simpler solution since this is the only part of the form that needs database validation before submitting the form.

This is what I have currently for my validation:


<script language="javascript">
<!--
function validate(theform){

var reCoverageCd1 = /^\d{2}[0N]{1}\w{1}[0]{2}/;
if (theform.coverage_cd1.value == ""){
alert("You forgot to fill in the Coverage Code Box1");
return (false);
}
else if (!reCoverageCd1.test(theform.coverage_cd1.value)) {
alert("You have entered an invalid coverage code in box 1.");
return (false);
}
}
//-->
</script>

miranda
08-12-2005, 05:54 PM
What about something like this?


<%
'get the unique alphavalues
Set oRS = oConn.Execute "SELECT DISTINCT fieldName FROM tableName WHERE thisField = 'someCondition';"
If Not oRS.EOF Then
Dim x : x = ""
Do Until oRS.EOF
x = x & oRS("fieldName") & "|"
oRS.MoveNext
Loop
End If
oRS.Close
Set oRS = Nothing
dim z : z = Len(x)
x = Left(x, z-1) 'remove the trailing |
%>
<script language="javascript">
<!--
function validate(theform){

var reCoverageCd1 = /^\d{2}[0N]{1}\[<%=x%>]{1}[0]{2}/;
if (theform.coverage_cd1.value == ""){
alert("You forgot to fill in the Coverage Code Box1");
return (false);
}
else if (!reCoverageCd1.test(theform.coverage_cd1.value)) {
alert("You have entered an invalid coverage code in box 1.");
return (false);
}
}
//-->
</script>

neocool00
08-12-2005, 06:25 PM
Because you can't mix ASP inside of a Javascript tag.

SpirtOfGrandeur
08-12-2005, 06:44 PM
Yes you can. I do it all over the place.

neocool00
08-12-2005, 11:01 PM
Well, I always thought you couldn't do that. So I'll give it try and let you know how it works.

oracleguy
08-12-2005, 11:08 PM
Yeah you can because by the time the Javascript runs the ASP code has already been executed and done away with.

miranda
08-13-2005, 04:41 PM
Because you can't mix ASP inside of a Javascript tag.

I mix server side code(asp and php) inside of client side Javascript all the time.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum