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 18
  1. #1
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Question User can't enter same value in text area more than once

    Need help with my text box. I want to make it so if i have several text boxes only one number can be entered once. So if i type the number 15 in the first text box and type 15 in the second text box an error will occur saying you cant enter the same number twice. Any idea how to do this?


    Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <script>
    function checkForInvalid(obj) {
    	obj.value = obj.value.replace(/[^0-9\-]|(-{2,})/gi, (RegExp.$1.indexOf("-") > -1) ? "-" : "");
    }
    
    </script>
    </head>
    
    <body>
    <form name="myForm">
    <input type="text" name="one" onkeyup="checkForInvalid(this)">
    <br>
    <input type="text" name="two" onkeyup="checkForInvalid(this)">
    <br>
    <input type="text" name="three" onkeyup="checkForInvalid(this)">
    </form>
    </body>
    </html>
    Thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Here you are:-

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    function checkForInvalid(obj) {
    obj.value = obj.value.replace(/[^0-9\-]|(-{2,})/gi, (RegExp.$1.indexOf("-") > -1) ? "-" : "");
    }
    
    var nums = [];
    var count = 0;
    
    function chkNoDups(which) {
    var x = which.value;
    if (x !="") {
    nums [count] = x;
    count ++;
    }
    var len = nums.length;
    for (var i =0; i <len-1; i++) {
    if (x == nums[i]) {
    alert ("That number "  + x  + " is a duplicate");
    which.value = "";
    }
    }
    }
    
    </script>
    </head>
    
    <body>
    <form name="myForm">
    <input type="text" name="one" onkeyup="checkForInvalid(this)" onblur = "chkNoDups(this)">
    <br>
    <input type="text" name="two" onkeyup="checkForInvalid(this)" onblur = "chkNoDups(this)">
    <br>
    <input type="text" name="three" onkeyup="checkForInvalid(this)" onblur = "chkNoDups(this)">
    </form>
    </body>
    
    </html>
    You say that the contents of your textboxes are numbers but according to your regex the user may enter 12-34-56. (A string). And your title refers to a textarea, but you code has textboxes. They are not the same thing.


    At least once per year, some group of scientists will become very excited and announce that whatever they announced last year about global warming is wrong.
    Last edited by Philip M; 10-26-2010 at 04:12 PM.

  • Users who have thanked Philip M for this post:

    connollyc4 (10-26-2010)

  • #3
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Philip M View Post
    Here you are:-

    var nums = [];
    var count = 0;

    function chkNoDups(which) {
    var x = which.value;
    if (x !="") {
    nums [count] = x;
    count ++;
    }
    var len = nums.length;
    for (var i =0; i <len-1; i++) {
    if (x == nums[i]) {
    alert ("That number " + x + " is a duplicate");
    which.value = "";
    }
    }
    }
    Thanks Philip M. Works good. Just one more thing with this. How can I make it so if I enter in 3 numbers in each text box (lets say 1, 2 and 3) and i change my mind and want to enter in 3, 2, 1 how can i do this without getting an error that I have duplicated these numbers. Right now once you use any number and delete it that number still gets saved and can no longer be used. Also, I fixed that (-) thing. I used this code before with a phone text box and forgot to modify it. Thanks for noticing. I realize now that I messed up on the title. I have no clue how to change that

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    You would have to rewrite the code to always check *all* the boxes each time. So that you could reinitialize the array each time.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Old Pedant View Post
    You would have to rewrite the code to always check *all* the boxes each time. So that you could reinitialize the array each time.
    Would it be easier to check to see if anything has been repeated on submit instead?

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    <shrug> Don't think it would much matter.

    onsubmit you'd have to loop through all, so it's the same code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    That is a bit trickier. Pity you did not specify that at the outset.

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    function checkForInvalid(obj) {
    obj.value = obj.value.replace(/[^0-9]/gi,"");
    }
    
    var nums = [];
    
    function chkNoDups(which,numb) {
    w = which;
    var val = which.value;
    nums [numb] = val;
    nums.unique();
    }
    
    Array.prototype.unique = function () { 
    var r = []; 
    o:for (var i = 0, n = this.length; i < n; i++)  { 
    for (var x = 0, y = r.length; x < y; x++) { 
    if (r[x] != "" && r[x] == this[i]) { 
    alert ("The number" + this[i] +  " is a duplicate!"); 
    w.value = "";
    r[x] = "";
    this[i] = "";
    continue o; 
    } 
    } 
    r[r.length] = this[i]; 
    } 
    return r; 
    } 
     
    </script>
    </head>
    
    <body>
    
    <form name="myForm">
    <input type="text" name="one" onkeyup="checkForInvalid(this)";  onblur = "chkNoDups(this,1)">
    <br>
    <input type="text" name="two" onkeyup="checkForInvalid(this)"  onblur = "chkNoDups(this,2)">
    <br>
    <input type="text" name="three" onkeyup="checkForInvalid(this)"  onblur = "chkNoDups(this,3)">
    </form>
    </body>
    
    </html>
    Last edited by Philip M; 10-26-2010 at 09:55 PM. Reason: Improved

  • Users who have thanked Philip M for this post:

    connollyc4 (10-26-2010)

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    For the record, here is a modified version with the numbers totaled:-

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    function checkForInvalid(obj) {
    obj.value = obj.value.replace(/[^0-9]/gi,"");
    }
    
    
    var nums = ["","","","",""];  // initialise
    
    function chkNoDups(which,numb) {
    w = which;
    var val = parseInt(which.value);
    nums [numb] = val;
    nums.unique();
    var len = nums.length;
    var tot = 0;
    
    for (var i =0; i<len; i++) {
    if (nums[i] !="") {
    tot = tot + nums[i];
    }
    }
    document.myForm.total.value = tot;
    }
    
    Array.prototype.unique = function () { 
    var r = []; 
    o:for (var i = 0, n = this.length; i < n; i++)  { 
    for (var x = 0, y = r.length; x < y; x++) { 
    if (r[x] != "" && r[x] == this[i]) { 
    alert ("The number" + this[i] +  " is a duplicate!"); 
    w.value = "";
    r[x] = "";
    nums[i] = "";
    continue o; 
    } 
    } 
    r[r.length] = this[i]; 
    } 
    return r; 
    } 
     
    </script>
    </head>
    
    <body>
    <form name="myForm">
    <input type="text" name="one" onkeyup="checkForInvalid(this)";  onblur = "chkNoDups(this,1)">
    <br>
    <input type="text" name="two" onkeyup="checkForInvalid(this)"  onblur = "chkNoDups(this,2)">
    <br>
    <input type="text" name="three" onkeyup="checkForInvalid(this)"  onblur = "chkNoDups(this,3)">
    <br>
    <input type = "text" name = "total" value = 0 readonly>
    </form>
    </body>
    
    </html>
    Last edited by Philip M; 10-27-2010 at 03:42 AM.

  • Users who have thanked Philip M for this post:

    connollyc4 (10-28-2010)

  • #9
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Philip M.
    Thank you.

    I have one last question. Would it be hard to make it so a user can only type in the numbers 1 - 16 meaning anything >16 will get an error?

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Revise the function as follows:-

    Code:
    function chkNoDups(which,numb) {
    w = which;
    var val = parseInt(which.value);
    if (val == 0 || val >16) {
    alert ("Invalid Entry - value entered must be 1 - 16")
    w.value = "";
    setTimeout('w.focus(); w.select();', 25);
    return false;
    }
    if (isNaN(val)) {val = 0}  // catch blank entries
    nums[numb] = val;
    nums.unique();
    var len = nums.length;
    // ..... and as before
    Each of your input textfields should include

    size = "2" maxlength = "2"

  • Users who have thanked Philip M for this post:

    connollyc4 (10-28-2010)

  • #11
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Philip M View Post
    That is a bit trickier. Pity you did not specify that at the outset.

    Code:
     
    
    var nums = [];
    
    function chkNoDups(which,numb) {
    w = which;
    var val = which.value;
    nums [numb] = val;
    nums.unique();
    }
    
    Array.prototype.unique = function () { 
    var r = []; 
    o:for (var i = 0, n = this.length; i < n; i++)  { 
    for (var x = 0, y = r.length; x < y; x++) { 
    if (r[x] != "" && r[x] == this[i]) { 
    alert ("The number" + this[i] +  " is a duplicate!"); 
    w.value = "";
    r[x] = "";
    this[i] = "";
    continue o; 
    } 
    } 
    r[r.length] = this[i]; 
    } 
    return r; 
    } 
     
    </script>
    </html>
    Sorry, I have one more issue. How can I make it so undefined isn't seen as a duplicate? If I start at the 7th text box (without filling in any other text box) and type in a number i get the pop up "The number undefined is a duplicate".


    Have any idea why it is doing that?

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Should be able to just change this line:
    Code:
        if (r[x] != "" && r[x] == this[i]) {
    to this:
    Code:
        if ( r[x] != null && this[i] != null && r[x] != "" && r[x] == this[i]) {
    But I don't see how any of this code handles letting the user put more than one value into a single <input>. Did you drop that requirement??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    A further slight modification/improvement:-

    Code:
    function chkNoDups(which,numb) {
    w = which;  // must be global
    var val = parseInt(w.value,10);  // ensure radix is 10
    if (isNaN(val)) {return false}  // catch blank entries
    w.value = val;
    if (val == 0 || val >16) {  // or whatever restrictions apply
    alert ("Invalid Entry - value entered must be 1 - 16");
    w.value = "";  // clear the field
    setTimeout('w.focus(); w.select();', 25);  // and refocus on it
    return false;
    }
    nums[numb] = val;
    nums.unique();
    var len = nums.length;
    // ..... and as before
    You must initialise all the boxes in the nums array with

    var nums = ["","","","","",""]; // initialise as many times as there are boxes (note - no comma after last "")
    so that var len = nums.length; returns the correct value.

  • #14
    New Coder
    Join Date
    Oct 2010
    Location
    Denver
    Posts
    26
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    For the record, here is a modified version with the numbers totaled:-

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    function checkForInvalid(obj) {
    obj.value = obj.value.replace(/[^0-9]/gi,"");
    }
    
    
    var nums = ["","","","",""];  // initialise
    
    function chkNoDups(which,numb) {
    w = which;
    var val = parseInt(which.value);
    nums [numb] = val;
    nums.unique();
    var len = nums.length;
    var tot = 0;
    
    for (var i =0; i<len; i++) {
    if (nums[i] !="") {
    tot = tot + nums[i];
    }
    }
    document.myForm.total.value = tot;
    }
    
    Array.prototype.unique = function () { 
    var r = []; 
    o:for (var i = 0, n = this.length; i < n; i++)  { 
    for (var x = 0, y = r.length; x < y; x++) { 
    if (r[x] != "" && r[x] == this[i]) { 
    alert ("The number" + this[i] +  " is a duplicate!"); 
    w.value = "";
    r[x] = "";
    nums[i] = "";
    continue o; 
    } 
    } 
    r[r.length] = this[i]; 
    } 
    return r; 
    } 
     
    </script>
    </head>
    
    <body>
    <form name="myForm">
    <input type="text" name="one" onkeyup="checkForInvalid(this)";  onblur = "chkNoDups(this,1)">
    <br>
    <input type="text" name="two" onkeyup="checkForInvalid(this)"  onblur = "chkNoDups(this,2)">
    <br>
    <input type="text" name="three" onkeyup="checkForInvalid(this)"  onblur = "chkNoDups(this,3)">
    <br>
    <input type = "text" name = "total" value = 0 readonly>
    </form>
    </body>
    
    </html>
    I found this helpful for my website as well. One thing that is happening for me is when you type in a number in the last text box and the same number in the text box above…… after getting the duplicate error and you type a different value,, all values underneath is ignored when calculating the total. Is this just me?

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Here is A COMPLETELY REVISED VERSION which as far as I can tell works perfectly.

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    function checkForInvalid(obj) {
    obj.value = obj.value.replace(/[^0-9]/gi,"");
    }
    
    var nums = ["","","","","","",""];  // initialise as many array variables (here 6) as there are input textboxes PLUS ONE
    
    function chkNoDups(which,numb) {
    w = which;  // must be global
    n = numb; // n must be global
    var val = parseInt(w.value,10);
    if (isNaN(val)) {return false}  // catch blank entries
    w.value = val; // write back to field
    
    if (val == 0 || val >16) {  // or whatever restrictions apply
    alert ("Invalid Entry - value entered must be 1 - 16")
    w.value = "";  // clear the field
    setTimeout('w.focus(); w.select();', 25);  // and refocus on it
    return false;
    }
    
    nums[numb] = val;
    testDups();
    
    // Calculate totals if required
    var len = nums.length; 
    var tot = 0;
    for (var i =0; i<len; i++) {
    if (nums[i] !="") {
    tot = tot + nums[i];
    }
    }
    document.myForm.total.value = tot;
    
    }
    
    function testDups() {
    var len = nums.length;
    for (var i = 1; i <len; i++) {
    for (var j = 2; j <len; j++) {
    if ((nums[i] !="") && (i!=j) && (nums[i] == nums[j])) {
    alert ("The number " + nums[j] + " is a duplicate!");
    nums[n] = "";
    w.value = "";
    }
    }
    }
    
    }
    
    </script>
    </head>
    
    <body>
    <form name="myForm">
    <input type="text" name="one" onkeyup="checkForInvalid(this)";  onblur = "chkNoDups(this,1)">
    <br>
    <input type="text" name="two" onkeyup="checkForInvalid(this)"  onblur = "chkNoDups(this,2)">
    <br>
    <input type="text" name="three" onkeyup="checkForInvalid(this)"  onblur = "chkNoDups(this,3)">
    <br>
    <input type="text" name="four" onkeyup="checkForInvalid(this)"  onblur = "chkNoDups(this,4)">
    <br>
    <input type="text" name="five" onkeyup="checkForInvalid(this)"  onblur = "chkNoDups(this,5)">
    <br>
    <input type="text" name="six" onkeyup="checkForInvalid(this)"  onblur = "chkNoDups(this,6)">
    
    <br>
    <input type = "text" name = "total" value = 0 readonly>
    </form>
    </body>
    
    </html>

  • The Following 2 Users Say Thank You to Philip M For This Useful Post:

    connollyc4 (11-07-2010), Rangers (10-29-2010)


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

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