Link to home
Start Free TrialLog in
Avatar of Phonebuff
PhonebuffFlag for United States of America

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..  
<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 --
Avatar of Gary
Gary
Flag of Ireland image

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
Avatar of Phonebuff

ASKER

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

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

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

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

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 ?  

    --
Meant nothing, shouldn't have been there.

Are you sure you are using the new code and not a cached version
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
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes,   go to dispatch.dapage.net  --

     Login with Demo   Password expert

============
You changed the regex - it should be with the backslash

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

==================
Try using this in place of the backslash

&#92;

Open in new window

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

--------------------
When adding it, in the visual editor, don't switch to the HTML editor before you save it.
I don't / did not == I just hit save -
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  }
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
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 -

    ---
Gary,

     Thank you, I am going to have to get better with jsfiddle as it might have saved me a lot of work --
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
You active theme's functions.php file.

The one you can get to from Appearance | Editor when logged into WP