Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Location
    NW Iowa
    Posts
    185
    Thanks
    9
    Thanked 0 Times in 0 Posts

    JS Form Math: reads spaces as words

    SOURCE LINK:
    http://www.lakesnewsshopper.com/submit.asp

    I have a javascript form where a user submits a classified ad. Problem is, that when they go to check the price of the ad, if they have put 2 spaces at the end of a sentence, it reads it as 1 extra word.

    Example:
    Code:
    my name is matt.  I have blonde hair.  = 9 words
    my name is matt. I have blonde hair. = 8 words
    Does anyone know how I can edit the code to ignore double spaces, and not count them as words?

    SOURCE LINK:
    http://www.lakesnewsshopper.com/submit.asp

    thanks for the help, you guys have been great help so far

    Matt

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,174
    Thanks
    203
    Thanked 2,550 Times in 2,528 Posts
    field.value = field.value.replace(/\s+/g , ' ');

    will replace multiple whitespaces by one whitespace.

  • #3
    Regular Coder
    Join Date
    Dec 2002
    Location
    NW Iowa
    Posts
    185
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Sorry Philip, I'm javascript-stupid. How do I implement that into my current code?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,174
    Thanks
    203
    Thanked 2,550 Times in 2,528 Posts
    You need to modify your page as follows:-

    // This collapses two or more whitespaces to one
    frm.Ad_Wording.value = frm.AdWording.value.replace(/\s+/g , ' ');
    // This will get a number of total words.
    wordCount=frm.Ad_Wording.value.split(" ");

  • #5
    Regular Coder
    Join Date
    Dec 2002
    Location
    NW Iowa
    Posts
    185
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Do I need to make a new function for that to work, or can I just toss it in somewhere?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,174
    Thanks
    203
    Thanked 2,550 Times in 2,528 Posts
    No, you cannot just toss it in anywhere.

    Add these two NEW LINES:-

    // This collapses two or more whitespaces to one
    frm.Ad_Wording.value = frm.AdWording.value.replace(/\s+/g , ' ');

    right in front of these two EXISTING LINES:-

    // This will get a number of total words.
    wordCount=frm.Ad_Wording.value.split(" ");

    This is within the existing function:-


    function chargeWords()
    {
    frm = document.form1;
    chargeWordCount = 0; // Zero-out chargeWordCount before re-calculating them
    wordLimit = 30; // Sets number of words before addt'l charges are added
    wordCost = .05; // Sets cost of addt'l words over wordLimit
    baseCost = 6; // Sets base cost of add


    // This collapses two or more whitespaces to one
    frm.Ad_Wording.value = frm.AdWording.value.replace(/\s+/g , ' ');

    // This will get a number of total words.
    wordCount=frm.Ad_Wording.value.split(" ");

    OK?

    You have added the following - cut it out!

    <script language="javascript" type="text/javascript">
    // This collapses two or more whitespaces to one
    frm.Ad_Wording.value = frm.AdWording.value.replace(/\s+/g , ' ');
    // This will get a number of total words.
    wordCount=frm.Ad_Wording.value.split(" ");
    </script>

    OK?
    Last edited by Philip M; 01-09-2003 at 09:11 PM.

  • #7
    Regular Coder
    Join Date
    Dec 2002
    Location
    NW Iowa
    Posts
    185
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Sorry Philip, I misunderstood your first reply.

    I put it in there how you stated, but now my "Calculate Ad Cost" button does not work.

    Please advise.

    Thanks again,
    Matt

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,174
    Thanks
    203
    Thanked 2,550 Times in 2,528 Posts
    I think an extra space has got into

    frm.Ad_Wording.value = frm.AdWording.value.replace(/\s+/g , ' ');

    the space after the g and before the comma.


    It should read

    frm.Ad_Wording.value = frm.AdWording.value.replace(/\s+/g, ' ');

    Try that!

  • #9
    Regular Coder
    Join Date
    Dec 2002
    Location
    NW Iowa
    Posts
    185
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Good eye, let me give that a try.

  • #10
    Regular Coder
    Join Date
    Dec 2002
    Location
    NW Iowa
    Posts
    185
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Sorry, the button still doesn't work.

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script type="text/javascript"> 
    <!-- 
    function countwords(x){
         var w = x.split(/\s+/); 
         return (w != "") ? w.length : 0;
    }
    // -->
    </script>
    
    <input type="text" onblur="alert(countwords(this.value))" />
    P.S. I left out the FORM tag, which is necessary in correct HTML, and other necessary tags, for testing purposes. If I'm not mistaken though (and I use forms 24/7 almost), this should be the solution you are looking for.
    Last edited by whammy; 01-10-2003 at 01:32 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,174
    Thanks
    203
    Thanked 2,550 Times in 2,528 Posts
    Whammy - thank you for your help, but you have lost us! Mattboy is a Javascript virgin and I am only Grade 2!

    Please can you sugguest why the line of code which I suggested is throwing an error?

    Mattboy - if you want to use Whammy's elegant script you will need to change the variable w to wordCount (note the capitalisation).


    That is:-

    <script type="text/javascript">
    <!--
    function countwords(x){
    var wordCount = x.split(/\s+/);
    return (wordCount != "") ? wordCount.length : 0;
    }
    // -->
    </script>


    Javascript is very sensitive and syntax and capitalisation errors will stop it working. Are you sure that in altering your script as I suggested you have not accidentally messed up something else?

  • #13
    New Coder
    Join Date
    Nov 2002
    Location
    London, UK
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Philip - You're regular expression was exactly correct and would do the job of condensing all multiple spaces into a single space so a correct length value could be obtained to costing.

    Whammys regular expression will split the string on finding multiple space thus obtaining a word count there and then.

    frm.Ad_Wording.value is null or not an object is the error message.

    Code:
    function chargeWords()
    {
    	frm = document.form1;
    	chargeWordCount = 0; // Zero-out chargeWordCount before re-calculating them
    	wordLimit = 30; // Sets number of words before addt'l charges are added
    	wordCost = .05; // Sets cost of addt'l words over wordLimit
    	baseCost = 6;   // Sets base cost of add
    	
    	// This collapses two or more whitespaces to one 
    	frm.Ad_Wording.value = frm.AdWording.value.replace(/\s+/g, ' '); 
    
    	// This will get a number of total words.
    	wordCount=frm.Ad_Wording.value.split(" ");
    	
    	// This will find out how many words are over 30.
    	if(wordCount.length >= wordLimit)
    	{
    		chargeWordCount=wordCount.length-wordLimit;
    	}
    	else
    	{
    		chargeWordCount = 0;
    	}
    
    	// If there are no words over 30, there is standard charge.
    	// Otherwise, charge 5 cents per page.
    	if (chargeWordCount < 1) chargeAmount=0.00;
    	else chargeAmount=chargeWordCount*wordCost;
    
    	//Add the $6.00 base charge
    	totalCost = parseFloat(baseCost) + parseFloat(chargeAmount);
    	
    	document.form1.amount.value=formatCurrency(totalCost);
    	alert('Your advertisement will cost '+formatCurrency(totalCost));
    }
    the highlighed lines of code refer to a frm.Ad_Wording when it should be frm.Ad_wording with a lowercase w. Also one the first line of highlighted code it refers to a frm.AdWording. So i think thats why it wasn't working rather than PhilipM's regular expression.

    Having said that using Whammys code would be far more effecient

    Adam
    Last edited by Adam20002; 01-10-2003 at 11:34 AM.

  • #14
    Regular Coder
    Join Date
    Dec 2002
    Location
    NW Iowa
    Posts
    185
    Thanks
    9
    Thanked 0 Times in 0 Posts
    So do I have to add the "onblur" value to my current textarea where the ad text is typed?

  • #15
    Regular Coder
    Join Date
    Dec 2002
    Location
    NW Iowa
    Posts
    185
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I did add the onblur value to my current textarea, but what I didn't realize is that it pops up an alert telling me how many words are in the box, when I already have an alert telling me how much the ad costs.

    View the current page:
    http://www.lakesnewsshopper.com/submit.asp

    And the page with Whammy's code added:
    http://www.lakesnewsshopper.com/submit2.asp

    Please advise on what to do.

    Thanks,
    Matt


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •