We help IT Professionals succeed at work.

Javascript to check values does not seem to execute --

146 Views
Last Modified: 2014-08-06
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 --
Comment
Watch Question

CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
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
CERTIFIED EXPERT

Author

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

CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

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

CERTIFIED EXPERT

Author

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

  --
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

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

CERTIFIED EXPERT

Author

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 ?  

    --
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
Meant nothing, shouldn't have been there.

Are you sure you are using the new code and not a cached version
CERTIFIED EXPERT

Author

Commented:
Yes, I am sure it is updating and I have cleared everything --
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
Working fine for me. Do you have a live link I can look at?
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
Yes,   go to dispatch.dapage.net  --

     Login with Demo   Password expert

============
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
You changed the regex - it should be with the backslash

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

Author

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, '');

==================
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
Try using this in place of the backslash

&#92;

Open in new window

CERTIFIED EXPERT

Author

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

--------------------
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
When adding it, in the visual editor, don't switch to the HTML editor before you save it.
CERTIFIED EXPERT

Author

Commented:
I don't / did not == I just hit save -
CERTIFIED EXPERT

Author

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  }
Jason C. LevineDon't talk to me.
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
Don't remember now, couple of lines were invalid js
CERTIFIED EXPERT

Author

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 -

    ---
CERTIFIED EXPERT

Author

Commented:
Gary,

     Thank you, I am going to have to get better with jsfiddle as it might have saved me a lot of work --
Jason C. LevineDon't talk to me.
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

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
Jason C. LevineDon't talk to me.
CERTIFIED EXPERT

Commented:
You active theme's functions.php file.

The one you can get to from Appearance | Editor when logged into WP
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.