www.webdeveloper.com
Results 1 to 11 of 11

Thread: remove spaces

  1. #1
    Join Date
    Jan 2010
    Posts
    127

    remove spaces

    I need to remove some spaces and set a variable based on length.

    So far I've got (from using remove spaces scripts and adding it into my function)

    PHP Code:
    var document.getElementById('iPostcode');
            
    p.split(' ').join('');
            var 
    postcode2;
            if ( 
    p.length == 6){
            
    postcode2 p.value.substring(0,3).toUpperCase();
            }
            if (
    p.length == 7){
            
    postcode2 p.value.substring(0,4).toUpperCase();
            } 
    Alas, it doesn't work. As in it doesn't seem to set postcode2 to any value, therefore the rest of the code gets ignored.

    Postcodes (without spaces) end up 6 or 7 in length, the reason I need to remove the space and set it this way is because otherwise 'IV3 conflicts with IV3X'

    JSLint says the code is fine, as in there are no errors, however it doesn't seem to do anything, so I'm guessing its still wrong.

    Any help would be great,

    Aravona
    L.U.N.E for anyone and everyone who has an interest in literature

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var p = document.getElementById('iPostcode').value
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Jan 2010
    Posts
    127
    Added that in, still gets nothing I'm afraid, it just lets everything through the form.
    L.U.N.E for anyone and everyone who has an interest in literature

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var p = document.getElementById('iPostcode').value;
            p.split(' ').join('');
            var postcode2;
            if ( p.length == 6){
            postcode2 = p.substring(0,3).toUpperCase();
            }
            if (p.length == 7){
            postcode2 = p.substring(0,4).toUpperCase();
            }
    At least 98% of internet users' DNA is identical to that of chimpanzees

  5. #5
    Join Date
    Jan 2010
    Posts
    127
    I added that in, still just passes through the system.

    PHP Code:
    function doValidation() {
    var 
    iv = ['IV3','IV4','IV5','IV6','IV7','IV8','IV9','IV10','IV11','IV12','IV13','IV14','IV15','IV16','IV17','IV18','IV19','IV20','IV21','IV22','IV23','IV24','IV25','IV26','IV27','IV28','IV29','IV30','IV31','IV32','IV33','IV34','IV35','IV36','IV37','IV38','IV39','IV40','IV41','IV42','IV43','IV44','IV45','IV46','IV47','IV48','IV49','IV50','IV51','IV52','IV53','IV54','IV55','IV56','IV63'];
            var 
    true;
           var 
    document.getElementById('iPostcode').value;
            
    p.split(' ').join('');
            var 
    postcode2;
            if ( 
    p.length == 6){
            
    postcode2 p.substring(0,3).toUpperCase();
            }
            if (
    p.length == 7){
            
    postcode2 p.substring(0,4).toUpperCase();
            }
            for ( var 
    i=0i<iv.lengthi++ ) {
            if ( 
    iv[i]=== postcode2 ) {
              
    false;
              
    alert('Your postcode requires extra shipping, please contact us: here');
            }
            }
        
                    return 
    b;
            
          } 
    Goes through the test form with:

    HTML Code:
        <form>
          Postcode: <input type="text" id="iPostcode" />
          <br />
          <input type="submit" value="Submit" onclick="return doValidation()" />
        </form>
    L.U.N.E for anyone and everyone who has an interest in literature

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
            for ( var i=0; i<iv.length; i++ ) {
            if ( postcode2.indexOf(iv[i])===0 ) {
              b = false;
              alert('Your postcode requires extra shipping, please contact us: here');
            }
            }
    At least 98% of internet users' DNA is identical to that of chimpanzees

  7. #7
    Join Date
    Jan 2010
    Posts
    127
    That worked for something like 'iv3 6kl' but not for the longer one of 'iv34 8kh' for example.

    Switched the if to an else if - I know some language get testy with several ifs using the same variable but it did nothing to help.
    L.U.N.E for anyone and everyone who has an interest in literature

  8. #8
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    if ( postcode2.indexOf(iv[i]+' ')===0 ) {
    At least 98% of internet users' DNA is identical to that of chimpanzees

  9. #9
    Join Date
    Jan 2010
    Posts
    127
    I'm afraid that didn't work either.

    typing in iv3 6kl works.
    typing in iv36kl works too.
    trying iv32 6kl doesn't.
    trying iv326kl doesn't either :S

    I don't see why this isn't behaving for the 7 length at all.
    L.U.N.E for anyone and everyone who has an interest in literature

  10. #10
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    function doValidation() {
    var iv = ['IV3','IV4','IV5','IV6','IV7','IV8','IV9','IV10','IV11','IV12','IV13','IV14','IV15','IV16','IV17','IV18','IV19','IV20','IV21','IV22','IV23','IV24','IV25','IV26','IV27','IV28','IV29','IV30','IV31','IV32','IV33','IV34','IV35','IV36','IV37','IV38','IV39','IV40','IV41','IV42','IV43','IV44','IV45','IV46','IV47','IV48','IV49','IV50','IV51','IV52','IV53','IV54','IV55','IV56','IV63'];
            var b = true;
    	var postcode2 = document.getElementById('iPostcode').value.replace(/(\w{3,4})(\w{3})/, "$1 $2").toUpperCase();
            for ( var i=0; i<iv.length; i++ ) {
            if ( postcode2.indexOf(iv[i]+' ')===0 ) {
              b = false;
              alert('Your postcode requires extra shipping, please contact us: here');
            }
            }
        
                    return b;
            
          }
    At least 98% of internet users' DNA is identical to that of chimpanzees

  11. #11
    Join Date
    Jan 2010
    Posts
    127
    Works perfectly! Thanks so much Fang.
    L.U.N.E for anyone and everyone who has an interest in literature

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles