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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick Question - Simple Answer 1

Status
Not open for further replies.

JrClown

Technical User
Oct 18, 2000
393
0
0
US
If I did not want to redirect the user to an ASP page telling them that a field in a form can not be left blank, but rather have a Pop-up messege box display "The Account Number field can not be blank"

I'm not sure of the syntax using the code below:

if request.form("accnumber") = "" then
response.redirect "accnumber.asp"
end if

Thanks in advance.
QUOTE OF THE DAY
Not to know if bad; not to wish to know is worse.
<%
Jr Clown
%>
 
Wouldn't you rather do that with javascript???

Just a form validation function that would return true or false -- wouldn't have to mess with server till you knew everything was ok --

<form onSubmit=&quot;return checkMe();&quot;>

function checkMe(){
if (document.formName.elementName.value == ''){
alert('The box can't be blank!');
return false
}
else
return true;
end if
}
 
sure that'll work too. And how would I change your suggestion to check 5 fields?? QUOTE OF THE DAY
Not to know if bad; not to wish to know is worse.
<%
Jr Clown
%>
 
First, I just looked at my syntax up there, and I did what I always do -- mix up java syntax with vb syntax. I'm sure you know, but 'end if' is not valid in javascript -- use the {}'s to enclose control statements --

Once you fix that, you could either make it search sequentially, and throw the alert when it finds the first invalid field entry, and if it makes it all the way through without returning (i.e. all fields are valid) it will just go ahead and return true, and the form will submit --

if (document.formName.elementName1.value == ''){
alert('Box1 can't be blank!');
return false;
}
if (document.formName.elementName2.value == ''){
alert('Box2 can't be blank!');
return false;
}
return true;


Or, you could do something like building up a string to see all the ones that were invalid, and then display your alert box at the end that would describe all the boxes that need a valid entry:

var alertString;
alertString = '';

if (document.formName.elementName1.value == ''){
alertString = alertString + '1, ';
}
if (document.formName.elementName2.value == ''){
alertString = alertString + '2 ';
}

if (alertString != ''){
alert('Box(es) ' + alertString + 'must be filled in!')
return false;
}
else{
return true;
}

You could probably be a little more creative with the string that you build, and how you present it to your users, but that is the general idea, at least.

hope it helps! :)
Paul Prewett

 
I appreciate you suggestion Link9, but I not familiar with JavaScripting at all I'm more comfortable with VBScript (the little I know, anyways :))


I just don't know how to display a message using this code. Oh by they way, I did not mention that I'm using this on our Intranet, so traffic I'm not concern with. Thanks

if request.form(&quot;accnumber&quot;) = &quot;&quot; then
response.redirect &quot;accnumber.asp&quot;
end if
QUOTE OF THE DAY
Not to know if bad; not to wish to know is worse.
<%
Jr Clown
%>
 
Fair enough --

Howabout this, then --

if request.form(&quot;accnumber&quot;) = &quot;&quot; then
response.redirect &quot;accnumber.asp?blankField=accnumber&quot;
end if

Then, on the accnumber (which I'm assuming is where they came from in the first place) will be something like this in the first part of the code:

dim blankField, showAlert
blankField = request.querystring(&quot;blankField&quot;)
if blankField = &quot;accnumber&quot; then
showAlert = 1
else
showAlert = 0
end if

Ok, then down in the head of your html, you'll need to have a little tiny javascript funtion that will pop up the alert box -- (you can do this in vbscript, too, but I'm not sure what the syntax is -- msgbox, maybe??)

It would look like this:

<script language=javascript>
function checkBlanks(){
var showAlert;
showAlert = <%=showAlert%>;
if (showAlert == 1){
alert('The Account Number field can not be blank');
{
}
</script>

Then, you would fire that function with the body onLoad event:

<body onLoad=&quot;checkBlanks();&quot;>

So that if it's the first time through, then there will be no querystring attached, and therefore no value for the ASP variable, showAlert, and subsequently the alert will not be shown...

If you know how to show client side vbscript alert boxes, then you could write a vbscript subroutine in the same way that I wrote the javascript one, and fire it on the body onLoad -- I just haven't used the client side vbscript, so I wouldn't want to steer you wrong on that one...

The above should work just fine, though --

Furthermore, if you wanted to check several different variables, you could expand the checking mechanism in the request.querystring to fill showAlert with different numbers and then check all those in the javascript function. Basically, you would redirect based on what field was the first one that was found to be blank, send the value (i.e. the blank field name) via querystring back to the accnumber.asp, and then check to see what value was in there in the function:

dim blankField, showAlert
blankField = request.querystring(&quot;blankField&quot;)
if blankField = &quot;accnumber&quot; then
showAlert = 1
elseif blankField = &quot;theNextOne&quot; then
showAlert = 2
elseif blankField = &quot;theOneAfterThat&quot; then
showAlert = 3
end if

and so on..

Then, your javascript function would look like this:

<script language=javascript>
function checkBlanks(){
var showAlert;
showAlert = <%=showAlert%>;
if (showAlert == 1){
alert('The Account Number field can not be blank');
}
if (showAlert == 2){
alert('The next field cannot be left blank');
{
if (showAlert == 3){
alert('The field after that cannot be left blank');
}
}
</script>

Gee, what a mouthful.

I really hope this gets you going in the right direction.

Let me know -- :)
Paul Prewett
 
It worked great Link9, Thanks QUOTE OF THE DAY
Not to know if bad; not to wish to know is worse.
<%
Jr Clown
%>
 
Link9,
I am using alert to return to a form if it is not filled out correctly. For example, I have a field for quantity that must be from 1 to 99. My code is:

temp = document.item0.QTY.value;
if (temp > 0 && temp < 100) {
...
} else {
alert (&quot;Select a numeric Quantity greater than 0 and less than 100&quot;);
document.item0.QTY.focus();
document.item0.QTY.select();
return false;

But when I click OK on the popup window, it goes back to the top of my web page instead of to this field in the form. What am I doing wrong?
 
Use just the document.item0.QTY.focus() line...
________
George, M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top