Phonebuff
asked on
Javascript to check values does not seem to execute --
I have some VERY old html that I am moving into a new environment and the form works, but the javascript to ensure that the text box is not empty or more than 400 characters does not seem to function -- (Yes it was done way back in FrontPage).
Now it looks like this..
I have tried a few variations of the onsubmitt with no luck..
Hope someone here see's my problem --
Now it looks like this..
<script language = "JavaScript" type="text/javascript" >
function FrontPage_Form1_Validator(theForm) {
if (theForm.pageText.length==0 ) {
alert("Please enter a message in the Text box.");
theForm.pageText.focus();
return (false);
}
// first remove all spaces using the following regex
StrToCheck= theForm.pageText.replace(/^s+|s+$/, '');
// then we check for the length of the string if its 0 or not
if( StrToCheck.length==0) {
alert("Please enter a message in the Text box.");
theForm.pageText.focus();
return (false);
}
if (theForm.pageText.value.length > 400) {
alert("Please enter at most 400 characters in the Text box.");
theForm.pageText.focus();
return (false);
}
return (true);
}
</script>
-----------------------
<form action="/cgi-bin/dapicgi.php" method="post" name="FrontPage_Form1" id="pagemsg" onsubmit="return autocomplete=off FrontPage_Form1_Validator(this)" >
I have tried a few variations of the onsubmitt with no luck..
Hope someone here see's my problem --
ASKER
Okay,
Changed it, but have the same result, it is passing an empty text box (pageText) --
Changed it, but have the same result, it is passing an empty text box (pageText) --
<form action="/cgi-bin/dapicgi.php" method="post" name="FrontPage_Form1" id="pagemsg" onsubmit="return FrontPage_Form1_Validator(this)" >
<input type="hidden" name="Server" value="snpp.dapage.net">
<p class="c1"><strong>Enter Page Text:</strong></p>
<p class="c1">
<textarea rows="5" name="pageText" cols="40" onkeyup="return taNCount(this, 200)">
</textarea></p>
<p class="c1"> Characters remaining : <span id=myCounter>200</span></p>
<p class="c1">
Select list content -
<select id="pageTarget" size="1" value="none" width="20" style="width: 200px">
---------------------------------
Replace your script with
<script type="text/javascript" >
function FrontPage_Form1_Validator(theForm) {
if (theForm.pageText.value.length==0 ) {
alert("Please enter a message in the Text box.");
theForm.pageText.focus();
return false;
}
// first remove all spaces using the following regex
StrToCheck= theForm.pageText.value.replace(/^s+|s+$/, '');
// then we check for the length of the string if its 0 or not
if( StrToCheck.length==0) {
alert("Please enter a message in the Text box.");
theForm.pageText.focus();
return false;
}
if (theForm.pageText.value.length > 400) {
alert("Please enter at most 400 characters in the Text box.");
theForm.pageText.focus();
return false;
}
}
</script>
ASKER
Gary,
flag=0 ??
Worked for the empty box but not if I just hit the space bar twice, which is what the replace() regular expression should be catching ..
--
flag=0 ??
Worked for the empty box but not if I just hit the space bar twice, which is what the replace() regular expression should be catching ..
--
<script language = "JavaScript" type="text/javascript" >
function FrontPage_Form1_Validator(theForm) {
if (theForm.pageText.value.length == 0) {
alert("Please enter a message in the Text box.");
theForm.pageText.focus();
return false;
}
// first remove all spaces using the following regex
StrToCheck = theForm.pageText.value.replace(/\s/g, '');
// then we check for the length of the string if its 0 or not
if (StrToCheck.length == 0) {
alert("Please enter a message in the Text box.");
theForm.pageText.focus();
return false;
}
if (theForm.pageText.value.length > 400) {
alert("Please enter at most 400 characters in the Text box.");
theForm.pageText.focus();
return false;
}
}
</script>
ASKER
Gary,
Tried that but , same issue with spaces in the text box.
In your suggestion above what did the flag=0 line do that I am now getting the proper empty box message ?
--
Tried that but , same issue with spaces in the text box.
In your suggestion above what did the flag=0 line do that I am now getting the proper empty box message ?
--
Meant nothing, shouldn't have been there.
Are you sure you are using the new code and not a cached version
Are you sure you are using the new code and not a cached version
ASKER
Yes, I am sure it is updating and I have cleared everything --
Working fine for me. Do you have a live link I can look at?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, go to dispatch.dapage.net --
Login with Demo Password expert
============
Login with Demo Password expert
============
You changed the regex - it should be with the backslash
StrToCheck= theForm.pageText.value.rep lace(/\s/g, '');
StrToCheck= theForm.pageText.value.rep
ASKER
:-( Though I had copied and pasted it ..
Actually, I am pasting it into a box for wp_head() the Genesis Theme under Wordpress, I save it and go back and look and the string has changed
StrToCheck= theForm.pageText.value.rep lace(/\s/g , '');
to this with no back slash.
StrToCheck= theForm.pageText.value.rep lace(/s/g, '');
==================
Actually, I am pasting it into a box for wp_head() the Genesis Theme under Wordpress, I save it and go back and look and the string has changed
StrToCheck= theForm.pageText.value.rep
to this with no back slash.
StrToCheck= theForm.pageText.value.rep
==================
Try using this in place of the backslash
\
ASKER
Nope took that out as well ... :-) But that was a great idea --
Thinking I might move the code out of the "form" and replace it with a #include <file> ...
--------------------
Thinking I might move the code out of the "form" and replace it with a #include <file> ...
--------------------
When adding it, in the visual editor, don't switch to the HTML editor before you save it.
ASKER
I don't / did not == I just hit save -
ASKER
Got it --
Will have to be very carefull with it but if I put in (/\\s one of the two backslashes stay in place, but if I update that page for any other reason it will take the remaining backslash out -- An I change the text message so I know it's that alert that is popping up..
Now I need to check that my Select is not empty an I am done at least this part -- Still not sure what you changed from my original function that it is working as designed now ..
if (theForm.pin.length == 0){ Array is empty }
Will have to be very carefull with it but if I put in (/\\s one of the two backslashes stay in place, but if I update that page for any other reason it will take the remaining backslash out -- An I change the text message so I know it's that alert that is popping up..
Now I need to check that my Select is not empty an I am done at least this part -- Still not sure what you changed from my original function that it is working as designed now ..
if (theForm.pin.length == 0){ Array is empty }
Actually, I am pasting it into a box for wp_head() the Genesis Theme under Wordpress
Probably shouldn't do that. Any submission into any form field in wp-admin gets run through the sanitizer and that's why you are seeing stuff get stripped. I would create a .js file with this script and call it via wp_enqueue_scripts() instead.
Don't remember now, couple of lines were invalid js
ASKER
Jason,
That sounds like I plan, but I am a WordPress rookie and have no idea where I find that call back..
Do I have to add a plug_in to use it ? http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
A search for a "New Plugin" turns up hundreds -- with the call back name -
---
That sounds like I plan, but I am a WordPress rookie and have no idea where I find that call back..
Do I have to add a plug_in to use it ? http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
A search for a "New Plugin" turns up hundreds -- with the call back name -
---
ASKER
Gary,
Thank you, I am going to have to get better with jsfiddle as it might have saved me a lot of work --
Thank you, I am going to have to get better with jsfiddle as it might have saved me a lot of work --
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well, I have a lot of functions.php files in a couple of directories --
./public_html/dispatch/wp- includes/f unctions.p hp
./public_html/dispatch/wp- content/th emes/twent yfourteen/ functions. php
./public_html/dispatch/wp- content/th emes/genes is/functio ns.php
./public_html/dispatch/wp- content/th emes/execu tive/funct ions.php
./public_html/dispatch/wp-
./public_html/dispatch/wp-
./public_html/dispatch/wp-
./public_html/dispatch/wp-
You active theme's functions.php file.
The one you can get to from Appearance | Editor when logged into WP
The one you can get to from Appearance | Editor when logged into WP
onsubmit="return autocomplete=off FrontPage_Form1_Validator(
Maybe post the whole code if that isn't the problem