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.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with checking user input

    _________
    Last edited by RachN92; 04-21-2011 at 10:36 AM.

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    String.indexOf() can be used to detect the occurence of one string within another. The "length" property holds the number of characters in a string.

    Code:
    alert("string".length); //6
    alert("string".indexOf("t")); //1
    alert("string".indexOf("g")); //5
    alert("string".indexOf("a")); //-1
    Last edited by gusblake; 04-20-2011 at 08:05 PM.

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by gusblake View Post
    String.indexOf() can be used to detect the occurence of one string within another. The "length" property holds the number of characters in a string.
    Thanks, sort of makes sense. I have to use a WHILE loop but am unsure of how to implement this

    It is an assignment for uni, so dont give me a full answer cos that would be cheating

    but could you give me an example say if the user has to enter their email address but it has to contain '@' and be a maximum of 30 characters...

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by RachN92 View Post
    Thanks, sort of makes sense. I have to use a WHILE loop but am unsure of how to implement this

    It is an assignment for uni, so dont give me a full answer cos that would be cheating

    but could you give me an example say if the user has to enter their email address but it has to contain '@' and be a maximum of 30 characters...
    Show the code you have written, then I am sure that someone will correct/improve it.

    But simply to check that an email address contains an @ sign is almost worthless as a validation. And why maximum of 30 characters? The maximum permitted is 254 characters, although I doubt if many actual ones are that long. I would have thought that max 50 characters would suffice.

  • #5
    New Coder
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Show the code you have written, then I am sure that someone will correct/improve it.

    But simply to check that an email address contains an @ sign is almost worthless as a validation. And why maximum of 30 characters? The maximum permitted is 254 characters, although I doubt if many actual ones are that long. I would have thought that max 50 characters would suffice.
    that may be so, but it was simply an example. I need to code it so a user enters a password that is not less than 7 characters long, contains a * symbol and starts with 'Z'

    i cannot show the code because i cant write it as that ^^^^ is what i am stuck on.
    I was hoping to receive some useful answers that might help me code it...

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    You said "It is an assignment for uni, so dont give me a full answer cos that would be cheating". Yet now you want us to write the code for you. It would have been preferable if you had made at least an attempt yourself.
    I do not see how your email question relates to your password question.
    "I need to code it so a user enters a password that is not less than 7 characters long, contains a * symbol and starts with 'Z'".



    Code:
    PASSWORD <input type = "text" id = "pwd" onblur = "chkit()">
    
    <script type = "text/javascript">
    function chkit() {
    var p = document.getElementById("pwd").value;
    if ((p.length <7) || (p.charAt(0)!= "Z") || (p.indexOf("*") == -1)) {
    alert ("Invalid password");
    }
    else {
    alert ("Password is OK");
    }
    
    }
    
    </script>
    You should be aware that if the hacker knows that your password must start with a Z and inckude a * then the password is less secure than the same number of unrestricted characters.

    Why have you deleted the first post in the thread?
    Last edited by Philip M; 04-21-2011 at 11:09 AM.

  • #7
    New Coder
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It didnt relate to it at all it was simply a change in scenario so you didnt give me the full answer for the REAL scenario i have been given. Also, i couldnt care less i it makes it less secure, its not going to be used its simply an exercise. Therefore i see no issue here.

    However, thanks for the code. i will try it


  •  

    Posting Permissions

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