File "jquery02.html"

Full Path: /home/analogde/www/Design/fileman/Fusion/Jquery/jquery02.html
File size: 3.72 KB
MIME-type: text/html
Charset: utf-8


<input type="text" id="txtName"/>
<span id="lblError" style="color: red"></span>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
	
		/*$("#txtName").keyup(function() 
		{
			var dInput = $(this).val();
			//alert(dInput);
			
			var str = document.getElementById('txtName').value 
			let firstChar = str.substring(0, 1); 
 
			console.log(" +++++++ " + firstChar);
			
			if( firstChar == ".")
			{
				$("#lblError").html("Pas de point ! ");
			}
			//alert(firstChar);
			
        //$(".dDimension:contains('" + dInput + "')").css("display","block");
		});
	*/
	
	
	 $('#txtName').keyup(function( e ) {
	 
		var keycode = e.keyCode ; //|| e.which;
		
		/*var str = document.getElementById('txtName').value; 
		if( str.length == 1 && keycode == "110")
		{
			console.log("Pas bon pour le premier caratére");
			this.value = "";
		}
		
		
		var position = str.indexOf('.');
		
		console.log(position);
		*/
		
		var str = document.getElementById('txtName').value; 
		var position = str.indexOf('.');
		if(position == 0)
		{
			var reste = str.substring(1)
			console.log("first" + " - " + reste);
			this.value = reste;
		}
		
		var str = document.getElementById('txtName').value; 
		var a = str.split('.');
        if (a.length > 2) {
            this.value = a.slice(0, 2).join('.');//remove all after second dot
            a[0] +='.'; this.value = a.join('');//only removes redundant dots
        }
		
/*	if (/\D/g.test(this.value))
  {
    // Filter non-digits from input value.
    this.value = this.value.replace(/\D/g, '');
  }*/
  
	  this.value = this.value.replace(/[^0-9.\.]/g,'');
		
		//(key >= 35 && key <= 40) ||
        //        (key >= 48 && key <= 57) ||
        //        (key >= 96 && key <= 105));

		// var tvalue = $( this ).val();
    
		//var toto = tvalue.substring(0, -1);
		
	//	console.log(toto);
	
		
	/* 
        var str = this.value;
		
		var str = document.getElementById('txtName').value 
		var firstChar = str.substring(0, 1); 
		
		if( firstChar == ".")
		{
			console.log(" Erreur ");
			this.value = "";
		}
		
        var a = str.split('.');
        if (a.length > 2) {
            this.value = a.slice(0, 2).join('.');//remove all after second dot
            //a[0] +='.'; this.value = a.join('');//only removes redundant dots
        }
		*/
		
		/*
		$("#lblError").html("");
		*/
		/* var regex = /^[0-9]+$/;
 
            //Validate TextBox value against the Regex.
            var isValid = regex.test(String.fromCharCode(keycode));
            if (!isValid) {
                $("#lblError").html("Only Alphabets and Numbers allowed.");
            }
 
            return isValid;
		*/
    });
	
	// https://www.aspsnippets.com/Articles/2694/Restrict-user-from-entering-Special-Characters-in-TextBox-using-jQuery/
	
	/*
	
        $("#txtName").keypress(function (e) {
            var keyCode = e.keyCode || e.which;
 
			var str = document.getElementById('txtName').value 
			let firstChar = str.substring(0, 1); 
			
			//var regex = new RegExp(/\./g)
			//var count = "This is some text .".match(regex).length;
 
			//alert(firstChar);
 
			$("#lblError").html("");
 
            //Regex for Valid Characters i.e. Alphabets and Numbers.
            var regex = /^[A-Za-z0-9.]+$/;
 
            //Validate TextBox value against the Regex.
            var isValid = regex.test(String.fromCharCode(keyCode));
            if (!isValid) {
                $("#lblError").html("Only Alphabets and Numbers allowed.");
            }
 
            return isValid;
			
			
        });
		*/
    });
</script>