Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbscript in form validation

Status
Not open for further replies.

ironyx

Technical User
Nov 13, 2001
134
US
I have a script:
sub check_input(myInput)
If document.getElementByID(myInput).value = "input" then
msgbox "This entry must be a number"
document.getElementByID(myInput).select
stat.cancel
end if
end sub

This pulls a variable from a form on another page, called through an onChange attached to the input. What I am trying to do it the same function, but with a specific reference to the actual document.myForm.myInput instead of the getElementsByID

I am a little confused (this is my first time working with VBScript). If I have a direct reference from the function to the form field on the other page, do I still have to pass the variable? The input type is a select drop down.
Please don't laugh at my attempt and confustion!

<select name="myInput" onChange="check_myInput(myInput.selectedindex')">

sub check_myInput(myInput)
If document.myForm.myInput.options(myinput).value = "input" then
msgbox "This entry must be a number"
document.myForm.myInput.select
stat.cancel
end if
end sub

Am I all mixed up? Any direction would be greatly appreciated!!!

Thanks,
Va. :)
 
A more concise and direct parameter passing can be done simply like this.
[tt]
<select name="myInput" onChange="check_myInput(me)">

sub check_myInput(obj)
If obj.value = "input" then
msgbox "This entry must be a number"
obj.focus
'stat.cancel 'don't understand
end if
end sub
[/tt]
This will rely less on the concrete form structure. The rest, "the other page..." etc etc, I don't understand much.

 
Okay, so I have a form on one page and the form validation vbscript in a second page.

The reason I was trying to code based on concrete form structure is because I am really trying to validate on two different fields in one validation. I thought if I could figure out how to reference one based on the document object, that I could figure out the rest. Like I mentioned in the other post you were kind enough to answer, I inherited this application and now have to do enhancements. So what I am really tying to do is:

Form drop down one:
1. If = particular value
2. If = any other value

Form drop down two:
1. Then ensure this field is populated
2. Then disabled

Only the first option is necessary, the second is a "nice to have". Some client side validation is already built in the vbscipt, so I am trying to learn that as quickly as I can and follow the logic of a peculiar programmer. I was hoping if I could get help on how to reference the form values in vbscript, I could kind of just "figure out" the rest.

I appreciate your help and time!!!

Thanks,
Va.

 
>so I have a form on one page and the form validation vbscript in a second page.
Impossible if you are meant for different page. I don't think it makes any difference you're using cf or asp. It is done server-side to relay appropriate "second" page according to different "first" page client's selections.

As to the reference to input elements' value by "name", there are numerous ways perfectly acceptable (in vbs):
[tt] document.myForm.myInput.value
document.forms("myForm").elements("myInput").value[/tt]
or combination of them. More exotic but still acceptable:
[tt] document.forms.myForm.elements.myInput.value[/tt]
IE accepts more shortcuts that I won't bother to use---it is too much of a shortcut---that I don't like.

If you reference them by "id", you can simply always stick to use document.getElementById("theid").value. Don't use shortcut like simply theid.value.
 
Do you mean what I have is impossible or validating two fields? I really do have a form on one page and the vbscript validation on another and it's still client side.

I cannot however get document.myForm.myInput.value to work because apparently for this two page thing to work you have to grab a variable and pass it to the other page?

-----WORKS-------
<select name="myInput" onChange="check_input(myInput)">

sub check_input(myInput)
If document.getElementByID(myInput).value = "input" then
msgbox "This entry must be a number"
document.getElementByID(myInput).select
stat.cancel
end if
end sub


-----DOESN'T WORK-------
<select name="myInput" id="def" onChange="check_input()">

sub check_input()
If document.myForm.myInput.value = "input" then
msgbox "This entry must be a number"
document.myForm.myInput.select
stat.cancel
end if
end sub

Well, I am at a loss... like I said, I actually am trying a round about way to validate on two different inputs in a form.

Thanks,
Va.
 
>-----WORKS-------
<select name="myInput" onChange="check_input(myInput)">
sub check_input(myInput)
If document.getElementByID(myInput).value = "input" then

It works? It must be an artist's work. If you really want to use that kind of syntax that I'm not impressed, do this.
[tt]
<select name="myInput" onChange="check_input(myInput)">
sub check_input(myInput)
If [blue]myInput[/blue].value = "input" then
[/tt]

>Do you mean what I have is impossible or validating two fields? I really do have a form on one page and the vbscript validation on another and it's still client side.
I mean what I said no anything else. If the second page is not spawned from the first client-side then no.

I have nothing more to add.
 
I really didn't mean for the question to sound obnoxious, it was a sincere question. I kind of have my hands tied with the way it was already written.
I think a lot of the issues is because the form is embedded in an xslt with this vbscript in another page. I have never seen anything like this and am quite perplexed myself.

I am normally a javascript person, so I thought this was some kind of weird functionality ability of vbscript, but apparently not. Anyway, thanks for your time!

Va. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top