Sample code in Edit Add -> No-Blanks-Allowed
Mike,
The following code works w/o any new code. The difference is that the new hook is in the Validator. That means, you want to have full control at the same time using the Validator. The following code do not use the Validator and just use simple Js to check for blanks in the ADD operation and return false. I only code for 4 elements covering TEXT and SELECT.
Frank
Dim ujs As String = "<" & "script language='Javascript'>" & _
"function noblanks(thisform) {" & vbCrLf & _
"if (thisform.ASPDB_ADD_1_0.value == ''){" & _
"alert('LastName cannot be blank');return false;}" & _
"else if (thisform.ASPDB_ADD_2_0.value == ''){" & _
"alert('FirstName cannot be blank');return false;}" & _
"else if (thisform.ASPDB_ADD_3_0.value == ''){" & _
"alert('Title cannot be blank');return false;}" & _
"else if (thisform.ASPDB_ADD_4_0.options[thisform['ASPDB_ADD_4_0'].selectedIndex].value == ''){" & _
"alert('Title of Courtesy cannot be blank');return false;}" & _
"return true;}<" & "/script>"
Dim J As New Tornado.z
With J
.dbQP = "U=1| D=NWIND| Q=employees| S=8| gdf=0,1,2| Ni=b5, add| bm=employees;0| TH=title=No Blank in Add - User JS Validate"
.dbEditValidateName = "noblanks(this)"
.dbSendHead = ujs
.dbCommonTables = "Index=TOC|Value=Mr.,Mrs.,Ms.,Dr. ; index=EID,FullName|sql=Select employeeid, Firstname & ' ' & Lastname from employees"
.dbLookUpFlds = "fi=ReportsTo| key=EID|look=Fullname"
.dbEditAddFlds = "1,2,3,fi=4|Ty=Selectbox+B|Text=TOC|Val=TOC,5,6,7, 8,9,10,11,12," & _
"fi=ReportsTo|ty=SELECTBOX+B|val=EID|tex=FullName, fi=Notes|type=TextArea|tag=COLS=25 ROWS=5"
.ASPdbNET()
End With
Add ID=fieldname tag to the Edit element
I have added an ID tag to Edit and GridEdit elements such that you can access the element via the following ->
"if (document.getElementById('LastName').value == ''){" & _
"alert('LastName cannot be blank');return false;}" & _
"else if (thisform.ASPDB_ADD_2_0.value == ''){" & _
"alert('FirstName cannot be blank');return false;}" & _
In order to access vis the ID, you need a recent browser that supports the getElementById which is the same requirement for all the AJAX stuff.
So, for normal edit, we now have ID=LastName and for Grid Edit, we have LastName_row. Much easier to work with.
Note: For foreign language installation and the fieldname is in foreign language, you MUST use the NAME like ASPDB_ADD_1_0 and not the Fieldname. This browser problem was the result of a very long debug session with a German user about 8 years ago and we discovered the debug.
See updated example below - The LastName and Notes fields are using the ID tag.
Frank
Dim ujs As String = "<" & "script language='Javascript'>" & _
"function noblanks(thisform) {" & vbCrLf & _
"if (document.getElementById('LastName').value == ''){" & _
"alert('LastName cannot be blank');return false;}" & _
"else if (thisform.ASPDB_ADD_2_0.value == ''){" & _
"alert('FirstName cannot be blank');return false;}" & _
"else if (thisform.ASPDB_ADD_3_0.value == ''){" & _
"alert('Title cannot be blank');return false;}" & _
"else if (thisform.ASPDB_ADD_4_0.options[thisform['ASPDB_ADD_4_0'].selectedIndex].value == ''){" & _
"alert('Title of Courtesy cannot be blank');return false;}" & _
"if (document.getElementById('Notes').value == ''){" & _
"alert('Notes cannot be blank');return false;}" & _
"return true;}<" & "/script>"
Dim J As New Tornado.z
With J
.dbQP = "U=1| D=NWIND| Q=employees| S=8| gdf=0,1,2| Ni=b5, add| bm=employees;0| TH=title=No Blank in Add - User JS Validate"
.dbEditValidateName = "noblanks(this)"
.dbSendHead = ujs
.dbCommonTables = "Index=TOC|Value=Mr.,Mrs.,Ms.,Dr. ; index=EID,FullName|sql=Select employeeid, Firstname & ' ' & Lastname from employees"
.dbLookUpFlds = "fi=ReportsTo| key=EID|look=Fullname"
.dbEditAddFlds = "1,2,3,fi=4|Ty=Selectbox+B|Text=TOC|Val=TOC,5,6,7, 8,9,10,11,12," & _
"fi=ReportsTo|ty=SELECTBOX+B|val=EID|tex=FullName, fi=Notes|type=TextArea|tag=COLS=25 ROWS=5"
.ASPdbNET()
End With
User validation Js code for classic ASP-db
For the Classic ASP-db users, the following code is equivalent to the Tornado code in checking for blanks in Edit. Actually you can perform any kind of validation besides checking for blanks by using the Js functions.
<HTML><HEAD>
<script language='Javascript'>
function noblanks(thisform) {
if (thisform.LastName.value == ''){
alert('LastName cannot be blank');return false;}
else if (thisform.FirstName.value == ''){
alert('FirstName cannot be blank');return false;}
else if (thisform.title.value == ''){
alert('Title cannot be blank');return false;}
else if (thisform.TitleOfCourtesy.options[thisform['TitleOfCourtesy'].selectedIndex].value == ''){
alert('Title of Courtesy cannot be blank');return false;}
if (thisform.Notes.value == ''){
alert('Notes cannot be blank');return false;}
return true;}
</script>
</HEAD>
<BODY>
<Center><H3>This example illustrates User Validate Js in Classic ASP-db<BR></Center>
<%
Set X=Server.CreateObject("ASP.db")
with X
.dbUnit = 2006
.dbMode="dual-horiz"
.dbGridDisplayFlds="0,1,2"
.dbDSN = "NWIND"
.dbNavigationItem="top,bottom,prev,next,add"
.dbSQL="SELECT * FROM EMPLOYEES"
.dbEditFlds = "1,2,3,4,Notes"
.dbEditParams="Tablename=employees,bookmarkflds=0, EditValidateName=noblanks(this)"
.dbEditDropFlds="4,mdb=nwind.mdb,employees,titleof courtesy,,ADDNULL"
.aspdb
end with
%>
</BODY></HTML>
Example in using Validator and User Js to check blank dropbox
You can cut/paste the following code into your VS WebForm or you can add the .aspx file header and footer to make it stand alone. The code illustrate user checking the drop box on top of the validator which does not cover drop boxes.
Dim ta As String = "<" & "script language='javascript'>" & vbCrLf
ta &= "function checkselect(thisform){" & vbCrLf
ta &= "if (thisform.ASPDB_UPDATE_16_0.options[thisform['ASPDB_UPDATE_16_0'].selectedIndex].value == '')" & vbCrLf
ta &= "{" & vbCrLf
ta &= "alert('ReportsTo Field cannot be Blank');" & vbCrLf
ta &= "return false;" & vbCrLf
ta &= "}" & vbCrLf
ta &= "else {return true;}" & vbCrLf
ta &= "}<" & "/script>" & vbCrLf
Dim Update As New Tornado.z
With Update
.dbUnit = 701
.dbMode = "ty=dual-horiz| sysind=t"
.dbSkin = 3
.dbDSN = "nwind.mdb"
.dbSQL = "Select * From employees"
.dbGridDisplayFlds = "0,1"
.dbValidatorParams = "code=/tornado/Jars|entry=false| UserValidateJs=checkselect(thisform)"
.dbEditUpdateFlds = "fi=0|ty=RONOUPdate,4,fi=HireDate|ty=TextCalen dar, " & _
"fi=HomePhone|ty=TEXT|mask=USPHONE|event=both|req= true|err=Must be " & _
"XXX-XXX-XXXX,fi=ReportsTo|ty=SELECTBOX+B|val=EID|tex=FullN ame,fi=Notes|" & _
"type=TextArea|tag=COLS=25 ROWS=5"
.dbNavigationItem = "top,bottom,prev,next,update"
.dbBookMark = "employees;0"
.dbFormMagicCell = "fi=HireDate|macro=#Hiredate::d#"
.dbCommonTables = "index=EID,First,FullName|sql=Select employeeid,Firstname," & _
"Firstname & ' ' & Lastname from employees"
.dbLookUpFlds = "fi=EmployeeID|key=EID|look=FullName,fi=Report sTo| key=EID|look=Fullname"
.dbTextHolder = "Title=Tornado Demo - Normal Update with Advanced Validation"
.dbSendHead = ta
.ASPdbNET()
End With