Help with javascript field validation?

Asked

Viewed 56 times

0

Friends what is happening is that it is passing spaces and repeated numbers, I do not know what to do :P

<form name="form" id="form" action="passo_01.php" method="post" onSubmit="return validasucesso()">

<td bgcolor="#F7F7F7" ><input name="table1" type="text" id="table1" lang="1" onKeyUp="javascript:pulacampo('table1','table2');" size="3" maxlength="4" onKeyPress="return SomenteNumero(event)" autofocus /></td>
  

function IsNumeric(){
    		myForm = document.getElementById("form");
    		var myArray=[];
    		for(i=0;i<myForm.elements.length;i++){
    			if(myForm.elements[i].type=='number'){
    				myArray[myArray.length]=myForm.elements[i].value;
    			}
    		}
    		myArray.sort();
    		for(i=0;i<myArray.length-1;i++){
    			if(myArray[i]==myArray[i+1]){
    				alert('Chaves repitidas no formulário!');
    				selectkey(myArray[i]);
    				return false;
    				break;
    				exit;
    			}
    		}
    		return true;
}

function validasucesso(){
	var i;
	var value;
	for (i = 1;i <=70;i ++) {
	   eval("value = document.form.table" + i + ".value");
    	if (value.length < 4)	{
			alert("Cartão de segurança inválido, verifique os dígitos corretamente."); 
			eval("document.form.table" + i +".focus()");
			return false;
		}		
		
		if (IsNumeric(value)) {
		}
		else
		{
			alert("Cartão de segurança inválido, verifique os dígitos corretamente."); 
			eval("document.form.table" + i +".focus()");
			return false;
		}
	}
		for (i = 1;i <=70;i ++) {
		   eval("value = document.form.table" + i + ".value");
		   eval("document.form.table" + i + ".value = value");
		}	
}

  • The code posted is no test conditions! Who is Somentenumero who is Pulacampo

  • @Leocaracciolo are validations that are working, I can post here for you !

  • Function Somentenumero(e){ var key=(window.Event)? Event.keycode:e. which; if((key > 47 && key < 58)) Return true; Else{ if (key != 8) Return false; Else Return true; } } Function pulacampo(idobj, idproximo) { var str = new String(Document.getElementById(idobj).value); var mx = new Number(Document.getElementById(idobj).maxlength); if (str.length == mx) { Document.getElementById(idproximo). Focus(); } }

1 answer

0

    $(function(){
      $('#smesp').bind('input', function(){
        $(this).val(function(_, v){
         return v.replace(/\s+/g, '');
        });
      });
    $('#smesp').bind('input', function(){
        $(this).val(function(_, v){
         return v.replace('99', '9');
        });
      });      
    $('#smesp').bind('input', function(){
        $(this).val(function(_, v){
         return v.replace('88', '8');
        });
      });      
    $('#smesp').bind('input', function(){
        $(this).val(function(_, v){
         return v.replace('77', '7');
        });
      });      
    $('#smesp').bind('input', function(){
        $(this).val(function(_, v){
         return v.replace('66', '6');
        });
      });      
    $('#smesp').bind('input', function(){
        $(this).val(function(_, v){
         return v.replace('55', '5');
        });
      });      
    $('#smesp').bind('input', function(){
        $(this).val(function(_, v){
         return v.replace('44', '4');
        });
      });      
    $('#smesp').bind('input', function(){
        $(this).val(function(_, v){
         return v.replace('33', '3');
        });
      });      
    $('#smesp').bind('input', function(){
        $(this).val(function(_, v){
         return v.replace('22', '2');
        });
      });      
    $('#smesp').bind('input', function(){
        $(this).val(function(_, v){
         return v.replace('11', '1');
        });
      });      
    $('#smesp').bind('input', function(){
        $(this).val(function(_, v){
         return v.replace('00', '0');
        });
      });      
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="smesp"></textarea>

  • he’s blocking, just not blocking the repeated numbers!

  • Relax, I’m thinking here.

  • Okay. I think it’s simple to read.

  • are 4 digits each field, are 50 fields, what I didn’t want is for the guy to type 50 fields 0000 understood...

  • I did what you said in the question (don’t let repeat spaces and numbers pass by). You should write in enough detail your question. Editing her if I have to.

  • Got it, and then how do I ride ? In all fields the id there ?

  • You should create another friendly question, being more specific in what you want. Other people in the future will consult this site in search of the same problem you are going through, so we have to keep everything organized here.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.