Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Javascript to check values does not seem to execute --

Posted on 2014-08-01
25
Medium Priority
?
127 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 --
0
Comment
Question by:Phonebuff
  • 12
  • 10
  • 3
25 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40234723
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
 
LVL 15

Author Comment

by:Phonebuff
ID: 40234756
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
 
LVL 58

Expert Comment

by:Gary
ID: 40234795
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Author Comment

by:Phonebuff
ID: 40234921
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
 
LVL 58

Expert Comment

by:Gary
ID: 40234941
<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
 
LVL 15

Author Comment

by:Phonebuff
ID: 40234962
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
 
LVL 58

Expert Comment

by:Gary
ID: 40234970
Meant nothing, shouldn't have been there.

Are you sure you are using the new code and not a cached version
0
 
LVL 15

Author Comment

by:Phonebuff
ID: 40234985
Yes, I am sure it is updating and I have cleared everything --
0
 
LVL 58

Expert Comment

by:Gary
ID: 40234990
Working fine for me. Do you have a live link I can look at?
0
 
LVL 58

Accepted Solution

by:
Gary earned 1600 total points
ID: 40234993
0
 
LVL 15

Author Comment

by:Phonebuff
ID: 40235009
Yes,   go to dispatch.dapage.net  --

     Login with Demo   Password expert

============
0
 
LVL 58

Expert Comment

by:Gary
ID: 40235027
You changed the regex - it should be with the backslash

StrToCheck= theForm.pageText.value.replace(/\s/g, '');
0
 
LVL 15

Author Comment

by:Phonebuff
ID: 40235067
:-(  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
 
LVL 58

Expert Comment

by:Gary
ID: 40235078
Try using this in place of the backslash

&#92;

Open in new window

0
 
LVL 15

Author Comment

by:Phonebuff
ID: 40235101
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
 
LVL 58

Expert Comment

by:Gary
ID: 40235111
When adding it, in the visual editor, don't switch to the HTML editor before you save it.
0
 
LVL 15

Author Comment

by:Phonebuff
ID: 40235123
I don't / did not == I just hit save -
0
 
LVL 15

Author Comment

by:Phonebuff
ID: 40235155
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40235164
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
 
LVL 58

Expert Comment

by:Gary
ID: 40235166
Don't remember now, couple of lines were invalid js
0
 
LVL 15

Author Comment

by:Phonebuff
ID: 40235186
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
 
LVL 15

Author Comment

by:Phonebuff
ID: 40235187
Gary,

     Thank you, I am going to have to get better with jsfiddle as it might have saved me a lot of work --
0
 
LVL 70

Assisted Solution

by:Jason C. Levine
Jason C. Levine earned 400 total points
ID: 40235203
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
 
LVL 15

Author Comment

by:Phonebuff
ID: 40235314
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40235388
You active theme's functions.php file.

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
How do you create a user-centered user experience on your website? And what are some things you should consider in the process?
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question