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

Creating a function

Status
Not open for further replies.

cwolgamott

Programmer
May 29, 2002
69
US
Hello. :) I am creating some ASP pages to add, change and delete. In my change pages, I need to check to see if they have entered a value in a specific text box. Then, I need to check to see if they have changed the value that was already in the text box when it was loaded up. If it was changed, I need to pop up a message that states that the number already exists in the database and set the focus to the text box. I have tried to create a function to do this. It is as follows:

function checktheboxes() {
if (document.form1.regulator.value == "")
{
alert("Please fill out the regulator field");
document.form1.regulator.focus();
return (false);
}
if((document.form1.regulator.value) != (rsRegDetails.Fields.Items("Regulator").Value)){
if ((document.form1.regulator.value) == (rs.Fields.Items("Regulator").Value))
{
alert("This regulator number already exists in the database.");
document.form1.regulator.focus();
return (false);
}
}
return true;
}

Then I put it in the form tag:

<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;admregend.asp&quot; onSubmit=&quot;return checktheboxes()&quot;>

However, this does not work. It does not check like it should. I would greatly appreciate any suggestions. :) Thanks.
 
Hi!

It seems that you use client-side JavaScript script to check form values. You cannot compare client-side form field value with server-side Recordset field value!!! Recordset is server-side object! If you want to do this - create some JavaScript variable and set its value to Recordset field value (do it while processing .ASP page, other words on server-side) and then compare the value of this variable with form field.

Something like:
ASP page (you prepare page before sending)
--------------------
...(some code)

var a=<%= rsRegDetails.Fields.Items(&quot;Regulator&quot;) %>;
function checktheboxes() {
if (document.form1.regulator.value == &quot;&quot;)
{
alert(&quot;Please fill out the regulator field&quot;);
document.form1.regulator.focus();
return (false);
}
if((document.form1.regulator.value) != a){
if ((document.form1.regulator.value) == a)
{
alert(&quot;This regulator number already exists in the database.&quot;);
document.form1.regulator.focus();
return (false);
}
}
return true;
}


...(some code)
-------------------- Good Luck! :)
 
Hello. Thank you so much for your response. :) I tried the code that you gave me, but it still did not work, unfortunately. Here is the code I put into my page:

var regNum = <%=(rsRegDetails.Fields(&quot;Regulator&quot;).Value) %>;
function checktheboxes() {

if (document.form1.regulator.value == &quot;&quot;)
{
alert(&quot;Please fill out the regulator field&quot;);
document.form1.regulator.focus();
return (false);
}
if((document.form1.regulator.value) != regNum){
if ((document.form1.regulator.value) == regNum)
{
alert(&quot;This regulator number already exists in the database.&quot;);
document.form1.regulator.focus();
return (false);
}
}
return true;
}

I would greatly appreciate some hints or suggestions as to what I might be doing incorrectly. Thanks again for your help. :)
 
I would do like this (remove string &quot;if((document.form1.regulator.value) != regNum)&quot;, because it's unnecessary) and make some other changes:


var regNum = <%= rsRegDetails(&quot;Regulator&quot;) %>;
function checktheboxes()
{
if (document.form1.regulator.value == &quot;&quot;)
{
alert(&quot;Please fill out the regulator field&quot;);
document.form1.regulator.focus();
return false;
}
if (document.form1.regulator.value == regNum)
{
alert(&quot;This regulator number already exists in the database.&quot;);
document.form1.regulator.focus();
return false;
}
return true;
}

If it doesn't work again, say what exactly doesn't work, what error message you got and so on.

Good Luck! :)
 
Hello. Thanks again for you response. :) Actually, I am not getting an error message, but it is not working how I want it to. Instead of not allowing them to update, it does allow them to. What is happening is that the page comes up with the text boxes filled in from the database based on what the user searches for. What I need to do is to check to see if they changed that box, if so, then I need to check to see if that value already exists in the database. If it does, then I need to pop up a message box to tell them that that number already exists in the database and set the focus to that text box and not allow them to update. I changed my coding like you suggested:

var regNum = <%= rsRegDetails(&quot;Regulator&quot;) %>;
function checktheboxes()
{
if (document.form1.regulator.value == &quot;&quot;)
{
alert(&quot;Please fill out the regulator field&quot;);
document.form1.regulator.focus();
return (false);
}
if (document.form1.regulator.value == regNum)
{
alert(&quot;This regulator number already exists in the database.&quot;);
document.form1.regulator.focus();
return (false);
}
return true;
}

But it still does not work. :( Maybe I am not doing the correct thing in the function to receive the correct results. I would greatly appreciate any suggestions or hints you may have as to what I might be doing wrong. Thanks again for all of your help. :)
 
1) Please tell me, does the script work if you send empty field (you should also have pop-up saying that empty values are not permitted)?

2) When you press submit do you have error icon on the left bottom of you browser?

3) Look into source of you page - what is written right after &quot;var regNum = &quot;? There should be some value from the BS... Good Luck! :)
 
Hello. :)

1) The script works if I send an empty field (I get the message box that tells me that it needs a value)

2) I do not get the error icon on the left bottom of my browser

3) The value that is in the regNum variable is what the user searches for. It does not change if you change what is in the text box.

I think what I need to do is to see if the user has changed what is in the text box. If so, then I need to loop through the recordset rs to find out if there is a record that matches with the number they have entered. If so, then I need to pop up a message box and set focus to the text box. However, I am unsure of how to check to see if it has changed and how to loop through the recordset to find out if it exists. I am new to using Javascript. I would greatly appreciate any suggestions or tips on how to do this or where to find out how to do this. Thanks again for all of your help. :)
 
so you mean that there is more than one value form field (regulator) can be compared to? I thought that there is only one... This way you need to check with JS only that the string is not empty, then submit the page, connect to DB, see if value exists or not, then send the page back with the message, that this value already exists... Good Luck! :)
 
Thank you so much for all of your help. :) I think I am getting much closer, but have run into a bit of a snag. I am using Response.redirect to go back to the other page, but am unable to figure out how to display a message box when the user gets redirected. Do I put it in the Redirect statement or do I somehow call it on the page that they get redirected to? I would greatly appreciate any suggestions or hints. :) Thanks again for all of your help.
 
If you use redirect method you may put some value into the URL after ? and get it with Request.QueryString

1.asp
Response.Redirect(&quot;file_name.asp?result=&quot; & result)

2.asp
if Request.QueryString(&quot;result&quot;) = &quot;yes&quot; then
'put here some code
elseif Request.QueryString(&quot;result&quot;) = &quot;no&quot; then
'put here some code
end if




Does it make sense? Good Luck! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top