Closed Thread
Results 1 to 10 of 10

Thread: Reading string with javascript

  1. #1
    TurboSRT4 is offline Newbie
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Reading string with javascript

    hello, I am fairly new to javascript and this is really the first dynamic site I am attempting. I am good with perl and thats it. I have noticed with javascript examples they return true or false........ what exactly does the value "true" or "false" get returned to? is it assigned a variable name?

    I am wondering a simple way to check if a var is a certain amount of digits like 10 for phone number and 5 for zip code.

    please see the registration page here iboost.biz

    when you submit the form it calls a function which returns the eroors in a popup box as you will see. The phone number is set as var p_num without calling another function what is a simple if statement to see if p_num is 10 digits. I already have it only accepting numbers.

    if (p_num ???????????){
    my_errors = my_errors + "Phone Number Must Contain 10 Digits";
    }

    thanks guys!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Reading string with javascript

    When something is returned if it's not assigned or done something with its return value is gone forever.

    Code:
    <scriopt>
    function 
    find() {
    return 
    "134";
    }
    document.write(find());
    if(
    find()=="134") {
    document.write("Yes!");
    }
    var 
    findvar find(); 

  4. #3
    TurboSRT4 is offline Newbie
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Re: Reading string with javascript

    Thank you that was a very simple and very useful explanation it makes a lot of sense. Any help with my question about containing a certain amount of digits or characters?

  5. #4
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Reading string with javascript

    Yes
    Code:
    <script>
    var 
    mystring1 "hello world";
    document.write(mystring1.length);
    </script> 

  6. #5
    TurboSRT4 is offline Newbie
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Re: Reading string with javascript

    im sorry man i dont quite understand that.....

    say i have....
    <script>
    function register(){
    var p_num = document.register.phone.value;
    if (p_num ????????){
    var error = "invalid phone number";
    }
    }
    </script>

    i need it to return var error with invalid phone number if it does not contain exactly 10 digits. I have googled it but get complicated and not straight forward answers. I just need something simple thanks

  7. #6
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Reading string with javascript

    Code:
    <script>
    function 
    register() {
        if(
    document.register.phone.value.length != 10) {
            
    document.write("Invalid phone number.");
            return 
    false;
        }
        return 
    true;
    }
    </script> 

  8. #7
    TurboSRT4 is offline Newbie
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Re: Reading string with javascript

    thanks a lot man

  9. #8
    TurboSRT4 is offline Newbie
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Re: Reading string with javascript

    lol sorry can you please explain what the hell return true and false does! I dont get it what is the point of returning true and false and how do u tell if it was returned true or fals lol sorry might sound like a dumb question but it would reallly explain a lot! thanks

  10. #9
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Reading string with javascript

    Depending on how your using you this you may not need it. If this was in the "submit" button and it returned false in it's "onclick" I believe it will not allow you to submit the form which is what you want.

    True/false is boolean values. If you returned "register()" you could test if it's true or false"

    Code:
    <script>
    function 
    register() {
        if(
    document.register.phone.value.length != 10) {
            
    document.write("Invalid phone number.");
            return 
    false;
        }
        return 
    true;
    }
    if(
    register()) {
        
    document.getElementById('errors').innerHTML "Correct value!";
    }
    </script> 

  11. #10
    TurboSRT4 is offline Newbie
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Re: Reading string with javascript

    gotcha thanks again man

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Reading text from a string in a special way
    By bojan_p87 in forum C# Programming
    Replies: 5
    Last Post: 02-10-2011, 02:35 PM
  2. not reading string? [help]
    By Aaron.H in forum Java Help
    Replies: 2
    Last Post: 01-23-2011, 11:54 PM
  3. Reading longest word within a string.
    By psycho in forum Pascal and Delphi
    Replies: 5
    Last Post: 04-18-2010, 06:45 PM
  4. Problem with reading text from files into a string
    By DarkLordoftheMonkeys in forum C and C++
    Replies: 7
    Last Post: 01-23-2010, 08:09 PM
  5. Reading a string from a text file?
    By MoonBoots89 in forum C and C++
    Replies: 3
    Last Post: 03-13-2009, 03:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

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