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..  
<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)" >

Open in new window


I have tried a few variations of the onsubmitt with no luck..  

Hope someone here see's my problem --
LVL 15
PhonebuffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
This is wrong - the bolded shouldn't there

onsubmit="return autocomplete=off FrontPage_Form1_Validator(this)"

Maybe post the whole code if that isn't the problem
0
PhonebuffAuthor Commented:
Okay,  

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">
---------------------------------

Open in new window

0
GaryCommented:
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>

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

PhonebuffAuthor Commented:
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 ..

  --
0
GaryCommented:
<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>

Open in new window

0
PhonebuffAuthor Commented:
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 ?  

    --
0
GaryCommented:
Meant nothing, shouldn't have been there.

Are you sure you are using the new code and not a cached version
0
PhonebuffAuthor Commented:
Yes, I am sure it is updating and I have cleared everything --
0
GaryCommented:
Working fine for me. Do you have a live link I can look at?
0
GaryCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PhonebuffAuthor Commented:
Yes,   go to dispatch.dapage.net  --

     Login with Demo   Password expert

============
0
GaryCommented:
You changed the regex - it should be with the backslash

StrToCheck= theForm.pageText.value.replace(/\s/g, '');
0
PhonebuffAuthor Commented:
:-(  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.replace(/\s/g, '');

to this with no back slash.

StrToCheck= theForm.pageText.value.replace(/s/g, '');

==================
0
GaryCommented:
Try using this in place of the backslash

&#92;

Open in new window

0
PhonebuffAuthor Commented:
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> ...

--------------------
0
GaryCommented:
When adding it, in the visual editor, don't switch to the HTML editor before you save it.
0
PhonebuffAuthor Commented:
I don't / did not == I just hit save -
0
PhonebuffAuthor Commented:
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  }
0
Jason C. LevineNo oneCommented:
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.
0
GaryCommented:
Don't remember now, couple of lines were invalid js
0
PhonebuffAuthor Commented:
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 -

    ---
0
PhonebuffAuthor Commented:
Gary,

     Thank you, I am going to have to get better with jsfiddle as it might have saved me a lot of work --
0
Jason C. LevineNo oneCommented:
No, it's much simpler than that.

In your functions.php file, add this:

// Enqueue my form script
add_action( 'wp_enqueue_scripts', 'phonebuff_validation' ); // phonebuff_validation is an example.
function phonebuff_validation() {
    wp_enqueue_script( 'pbvalidate', get_stylesheet_directory_uri() . '/js/pbvalidate.js', '', '', false );
}

Open in new window


The above will look for a script in this location:

child-theme-directory/js/pbvalidate.js

See:

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

for what each parameter means.
0
PhonebuffAuthor Commented:
Well,  I have a lot of functions.php files in a couple of directories --

./public_html/dispatch/wp-includes/functions.php
./public_html/dispatch/wp-content/themes/twentyfourteen/functions.php
./public_html/dispatch/wp-content/themes/genesis/functions.php
./public_html/dispatch/wp-content/themes/executive/functions.php
0
Jason C. LevineNo oneCommented:
You active theme's functions.php file.

The one you can get to from Appearance | Editor when logged into WP
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.