Skip to content Skip to sidebar Skip to footer

Generate An Error If Number Is Repeated In A Form

I have a web form for admin purposes where the user can change the order that a group of records is shown on a webpage. For example: A table (tblStuff) in a database has three fiel

Solution 1:

Let's suppose you have form like this (notice all the inputs has the same class):

<form id="myForm" method="POST" action"someUrl">
    <inputtype="text" class="recordPosition"></input>
    <inputtype="text" class="recordPosition"></input>
    <inputtype="text" class="recordPosition"></input>
    <inputtype="text" class="recordPosition"></input>
</form>

You could do with jQuery something like this:

 $(document).ready(function(){

 $(".recordPosition").on("blur", function(){
     var allFieldsForOrder = $('.recordPosition');
     var count = 0;
     var i=0//console.log(allFieldsForOrder.length );while((i<allFieldsForOrder.length) && (count < 2)){
             if ($(allFieldsForOrder[i]).val()===$(this).val())
            {
             count++                 
            }
            i++;
     } 

     if (count==2){
        alert("A duplicated value"); returnfalse;}
    }); 
 });

For the html you posted you can use this: Notice I don't store the position of the duplicated the value.

$(document).ready(function(){
    //console.log($("input[type='text'][name^='PositionNumber'").length);
    $("input[type='text'][name^='PositionNumber'").each(function(){
         $(this).on("blur", function(){

         var allFieldsForOrder = $("input[type='text'][name^='PositionNumber'");
         var count = 0;
         var i=0while((i<allFieldsForOrder.length) && (count < 2)){
                if ($(allFieldsForOrder[i]).val()===$(this).val())
                {
                 count++                 
                }
                i++;
         }

         if (count==2){
            alert("A duplicated value"); 
         }

       });
    });
  });

For the code above we assume you want to check for all the fields where the attribute name starts with the string "PositionNumber"

I will try to reduce the code later, I think there's a shortest way to check if is duplicated the "RecordPosition" value, but need to test some ideas.

This will be your solution (one of them):

  $(document).ready(function(){


    $('form').on("submit",function(){
            var tempArray=[];
            $("input[type='text'][name^='PositionNumber'").each(function(){
                tempArray.push($(this).val());
            });

            var i=0;
            var duplicated=false;
            var currentElement;
            while((tempArray.length >= 0) && (duplicated==false)){                  
                //pop it out from the array
               currentElement = tempArray.pop();
               duplicated = tempArray.indexOf(currentElement)          

            }

            //after you can use "duplicated" to cancel the submitif (duplicated){                    
                alert("duplicated value:" + currentElement);
                returnfalse;
            }

    });
  });

I shorter version:

$(document).ready(function(){           
 $('form').on("submit",function(){
         var tempArray=[];            
        var exists=0;
         $("input[type='text'][name^='PositionNumber'").each(function(){
             exists = tempArray.indexOf($(this).val());             
             if (exists>=0){                    
                 returnfalse;//break the loop
             }
             tempArray.push($(this).val());                  
         });

         //after you can use "exist" to check if duplicated and retrieve the value to cancel the submitif (exists>=0){                   
             alert("duplicated value:" + tempArray[exists]);                
         } else{
             alert("no duplicated value:");
         }

        returnfalse;            
 }); 
});

Solution 2:

If you want to prevent duplicate values in RecordPosition no matter how you insert/update them you can create a unique constraint this column

CREATEUNIQUE INDEX uq_idx_RecordPosition ON tblStuff(RecordPosition);

Here is SQLFiddle demo

Solution 3:

If you're trying to do some client-side validation, you'd have to build an array that contains all the RecordPosition values.

Once you have that, you can check the array for duplicates. This has been asked a couple of times on SO: Easiest way to find duplicate values in a JavaScript array

Unfortunately I can't help any more than that because you don't include any code that shows how this is structured on your web page

Solution 4:

Check before inserting data in to data base

ex: recordposition value 3 --> 1 then pass value 1

SELECT * FROM tblStuff Where RecordPosition=1

if record exist then give message to user this position is exist

Post a Comment for "Generate An Error If Number Is Repeated In A Form"