Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    RegEx: errore Mixed spaces and tabs

    Salve a tutti, controllando attraverso il sito http://www.jslint.com/ mi sono accorto che nello script che utilizzo ci sono degli errori uno di questi è nelle espressioni regolari postate di seguito.
    L'errore è :
    Problem at line 18 character 2: Mixed spaces and tabs.
    codice:
    var dateRegEx = /^\d{1,2}\/\d{1,2}\/\d{2}|\d{4}$/;
    Potreste dirmi qual'è l'errore per favore?
    Grazie mille...

  2. #2
    potete aiutarmi per favore?grazie mille come sempre

  3. #3
    ragazzi, mi dite per favore dov'è l'errore, che da solo non riesco?
    grazie ancora

  4. #4
    Non c'è. O, perlomeno, da quella riga non si desume dove possa essere. Tieni presente che la console degli errori può identificare come linea numero tal dei tali una riga che in realtà sta una riga sopra o una riga sotto.

    Presumento quella sia la riga interessata, dipende anche da che contesto viene invocata.
    Una espressione regolare in sè è valida quanto una altra, e quella sembra essere finalizzata a controllare il formato di una data espressa in numeri e separata da slashes:
    01/01/2011

    Come viene invocata quella riga, in che contesto: una espressione regolare è concepita per essere usata per cui da qualche parte nell' html viene invocata, e nel javascript da qualche parte è assegnata a un metodo (che non sappiamo quale sia, può essere match, exec, test: non so): è a quel punto che sale l'errore, per cui è quel punto che bisgna conoscere più che la sola espressione regolare.

    Bisognerebbe sapere di più sul contesto e avere un po' piu di codice.

  5. #5
    grazie per la risposta.ho un'altra curiosità.
    Questo controllo è inserito all'interno di un calendario, dove, al click su una input text di un form, si apre il calendario e scegliendo una data, la stessa va ad inserirsi in automatico dentro la stessa input text.
    Questo calendario è formattato in due select option, dove comprendono mese e anno e nelle celle della tabella sn riportati i giorni.

    La data inizialmente aveva questo formato mese/giorno/anno, ma l'ho cambiarta in giorno/mese/anno.
    Qui la porzione di script:
    codice:
    jQuery.fn.simpleDatepicker.formatOutput = function (dateObj) {
    return  dateObj.getDate() + "/" + (dateObj.getMonth()+1) + "/" + dateObj.getFullYear();	
    };
    Come detto cliccando nella input text si apre il calendario,il quale a seconda della data che è dentro la input text, la ricorda.

    Quindi se ho scelto la data 2(gg)/1(mm)/2011(aaaa) dovrebbe riportarla all'interno della tabella del calendario, invece interpreta il 2 come mese e l'1 come giorno, sballando cosi l'apertura del calendario e quindi come se avessi scelto il primo febbraio.

    Posto tutto lo script, perchè non so da dove iniziare:
    codice:
    (function($) { 
            var today = new Date(); // used in defaults
    	var months = 'Gennaio,Febbraio,Marzo,Aprile,Maggio,Giugno,Luglio,Agosto,Settembre,Ottobre,Novembre,Dicembre'.split(',');
    	var monthlengths = '31,28,31,30,31,30,31,31,30,31,30,31'.split(',');
      	var dateRegEx = /^\d{1,2}\/\d{1,2}\/\d{2}|\d{4}$/;
    	var yearRegEx = /^\d{4,4}$/;
    
        $.fn.simpleDatepicker = function(options) {
    
    	var opts = jQuery.extend({}, jQuery.fn.simpleDatepicker.defaults, options);
    		
    		function setupYearRange () {
    			
    			var startyear, endyear;  
    			if (opts.startdate.constructor === Date) {
    				startyear = opts.startdate.getFullYear();
    			} else if (opts.startdate) {
    				if (yearRegEx.test(opts.startdate)) {
    				startyear = opts.startdate;
    				} else if (dateRegEx.test(opts.startdate)) {
    					opts.startdate = new Date(opts.startdate);
    					startyear = opts.startdate.getFullYear();
    				} else {
    				startyear = today.getFullYear();
    				}
    			} else {
    				startyear = today.getFullYear();
    			}
    			opts.startyear = startyear;
    			
    			if (opts.enddate.constructor == Date) {
    				endyear = opts.enddate.getFullYear();
    			} else if (opts.enddate) {
    				if (yearRegEx.test(opts.enddate)) {
    					endyear = opts.enddate;
    				} else if (dateRegEx.test(opts.enddate)) {
    					opts.enddate = new Date(opts.enddate);
    					endyear = opts.enddate.getFullYear();
    				} else {
    					endyear = today.getFullYear();
    				}
    			} else {
    				endyear = today.getFullYear();
    			}
    			opts.endyear = endyear;	
    		}
    		setupYearRange();
    		
    		function newDatepickerHTML () {
    			var i;
    			var years = [];
    			
    			for (i = 0; i <= opts.endyear - opts.startyear; i ++) years[i] = opts.startyear + i;
    	
    			var table = jQuery('<table class="datepicker" cellpadding="0" cellspacing="0"></table>');
    			table.append('<thead></thead>');
    			table.append('<tfoot></tfoot>');
    			table.append('<tbody></tbody>');
    			
    				// month select field
    				var monthselect = '<select name="month">';
    				for (var i in months) monthselect += '<option value="'+i+'">'+months[i]+'</option>';
    				monthselect += '</select>';
    			
    				// year select field
    				var yearselect = '<select name="year">';
    				for (var i in years) yearselect += '<option>'+years[i]+'</option>';
    				yearselect += '</select>';
    			
    			jQuery("thead",table).append('<tr class="controls"><th colspan="7"><span class="prevMonth">&laquo;</span>'+monthselect+yearselect+'<span class="nextMonth">&raquo;</span></th></tr>');
    			//jQuery("thead",table).append('<tr class="days"><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr>');
    			jQuery("thead",table).append('<tr class="days"><th>D</th><th>L</th><th>M</th><th>Mer</th><th>G</th><th>V</th><th>S</th></tr>');
    			jQuery("tfoot",table).append('<tr><td colspan="2"><span class="today">today</span></td><td colspan="3"></td><td colspan="2"><span class="close">close</span></td></tr>');
    			for (var i = 0; i < 6; i++) jQuery("tbody",table).append('<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>');	
    			return table;
    			
    		}
    		
    		/** get the real position of the input (well, anything really) **/
    		//http://www.quirksmode.org/js/findpos.html
    		function findPosition (obj) {
    			var curleft = curtop = 0;
    			if (obj.offsetParent) {
    				do { 
    					curleft += obj.offsetLeft;
    					curtop += obj.offsetTop;
    				} while (obj = obj.offsetParent);
    				return [curleft,curtop];
    			} else {
    				return false;
    			}
    		}
    		
    		/** load the initial date and handle all date-navigation **/
    		// initial calendar load (e is null)
    		// prevMonth & nextMonth buttons
    		// onchange for the select fields
    		function loadMonth (e, el, datepicker, chosendate) {
    			
    			// reference our years for the nextMonth and prevMonth buttons
    			var mo = jQuery("select[name=month]", datepicker).get(0).selectedIndex;
    			var yr = jQuery("select[name=year]", datepicker).get(0).selectedIndex;
    			var yrs = jQuery("select[name=year] option", datepicker).get().length;
    			
    			// first try to process buttons that may change the month we're on
    			if (e && jQuery(e.target).hasClass('prevMonth')) {				
    				if (0 == mo && yr) {
    					yr -= 1; mo = 11;
    					jQuery("select[name=month]", datepicker).get(0).selectedIndex = 11;
    					jQuery("select[name=year]", datepicker).get(0).selectedIndex = yr;
    				} else {
    					mo -= 1;
    					jQuery("select[name=month]", datepicker).get(0).selectedIndex = mo;
    				}
    			} else if (e && jQuery(e.target).hasClass('nextMonth')) {
    				if (11 == mo && yr + 1 < yrs) {
    					yr += 1; mo = 0;
    					jQuery("select[name=month]", datepicker).get(0).selectedIndex = 0;
    					jQuery("select[name=year]", datepicker).get(0).selectedIndex = yr;
    				} else { 
    					mo += 1;
    					jQuery("select[name=month]", datepicker).get(0).selectedIndex = mo;
    				}
    			}
    			
    			// maybe hide buttons
    			if (0 == mo && !yr) jQuery("span.prevMonth", datepicker).hide(); 
    			else jQuery("span.prevMonth", datepicker).show(); 
    			if (yr + 1 == yrs && 11 == mo) jQuery("span.nextMonth", datepicker).hide(); 
    			else jQuery("span.nextMonth", datepicker).show(); 
    			
    			// clear the old cells
    			var cells = jQuery("tbody td", datepicker).unbind().empty().removeClass('date');
    			
    			// figure out what month and year to load
    			var m = jQuery("select[name=month]", datepicker).val();
    			var y = jQuery("select[name=year]", datepicker).val();
    			var d = new Date(y, m, 1);
    			var startindex = d.getDay();
    			var numdays = monthlengths[m];
    			
    			// http://en.wikipedia.org/wiki/Leap_year
    			if (1 == m && ((y%4 == 0 && y%100 != 0) || y%400 == 0)) numdays = 29;
    			
    			// test for end dates (instead of just a year range)
    			if (opts.startdate.constructor == Date) {
    				var startMonth = opts.startdate.getMonth();
    				var startDate = opts.startdate.getDate();
    			}
    			if (opts.enddate.constructor == Date) {
    				var endMonth = opts.enddate.getMonth();
    				var endDate = opts.enddate.getDate();
    			}
    			
    			// walk through the index and populate each cell, binding events too
    			for (var i = 0; i < numdays; i++) {
    			
    				var cell = jQuery(cells.get(i+startindex)).removeClass('chosen');
    				
    				// test that the date falls within a range, if we have a range
    				if ( 
    					(yr || ((!startDate && !startMonth) || ((i+1 >= startDate && mo == startMonth) || mo > startMonth))) &&
    					(yr + 1 < yrs || ((!endDate && !endMonth) || ((i+1 <= endDate && mo == endMonth) || mo < endMonth)))) {
    				
    					cell
    						.text(i+1)
    						.addClass('date')
    						.hover(
    							function () { jQuery(this).addClass('over'); },
    							function () { jQuery(this).removeClass('over'); })
    						.click(function () {
    							var chosenDateObj = new Date(jQuery("select[name=year]", datepicker).val(), jQuery("select[name=month]", datepicker).val(), jQuery(this).text());
    							closeIt(el, datepicker, chosenDateObj);
    						});
    						
    					// highlight the previous chosen date
    					if (i+1 == chosendate.getDate() && m == chosendate.getMonth() && y == chosendate.getFullYear()) cell.addClass('chosen');
    				}
    			}
    		}
    		
    		/** closes the datepicker **/
    		// sets the currently matched input element's value to the date, if one is available
    		// remove the table element from the DOM
    		// indicate that there is no datepicker for the currently matched input element
    		function closeIt (el, datepicker, dateObj) { 
    			if (dateObj && dateObj.constructor == Date)
    				el.val(jQuery.fn.simpleDatepicker.formatOutput(dateObj));
    			datepicker.remove();
    			datepicker = null;
    			jQuery.data(el.get(0), "simpleDatepicker", { hasDatepicker : false });
    		}
    
            // iterate the matched nodeset
            return this.each(function() {
    			
                // functions and vars declared here are created for each matched element. so if
                // your functions need to manage or access per-node state you can defined them
                // here and use $this to get at the DOM element
    			
    			if ( jQuery(this).is('input') && 'text' == jQuery(this).attr('type')) {
    
    				var datepicker; 
    				jQuery.data(jQuery(this).get(0), "simpleDatepicker", { hasDatepicker : false });
    				
    				// open a datepicker on the click event
    				jQuery(this).click(function (ev) {
    											 
    					var $this = jQuery(ev.target);
    					
    					if (false == jQuery.data($this.get(0), "simpleDatepicker").hasDatepicker) {
    						
    						// store data telling us there is already a datepicker
    						jQuery.data($this.get(0), "simpleDatepicker", { hasDatepicker : true });
    						
    						// validate the form's initial content for a date
    						var initialDate = $this.val();
    						
    						if (initialDate && dateRegEx.test(initialDate)) {
    							var chosendate = new Date(initialDate);
    						} else if (opts.chosendate.constructor == Date) {
    							var chosendate = opts.chosendate;
    						} else if (opts.chosendate) {
    							var chosendate = new Date(opts.chosendate);
    						} else {
    							var chosendate = today;
    						}
    							
    						// insert the datepicker in the DOM
    						datepicker = newDatepickerHTML();
    						jQuery("body").prepend(datepicker);
    						
    						// position the datepicker
    						var elPos = findPosition($this.get(0));
    						var x = (parseInt(opts.x) ? parseInt(opts.x) : 0) + elPos[0];
    						var y = (parseInt(opts.y) ? parseInt(opts.y) : 0) + elPos[1];
    						jQuery(datepicker).css({ position: 'absolute', left: x, top: y });
    					
    						// bind events to the table controls
    						jQuery("span", datepicker).css("cursor","pointer");
    						jQuery("select", datepicker).bind('change', function () { loadMonth (null, $this, datepicker, chosendate); });
    						jQuery("span.prevMonth", datepicker).click(function (e) { loadMonth (e, $this, datepicker, chosendate); });
    						jQuery("span.nextMonth", datepicker).click(function (e) { loadMonth (e, $this, datepicker, chosendate); });
    						jQuery("span.today", datepicker).click(function () { closeIt($this, datepicker, new Date()); });
    						jQuery("span.close", datepicker).click(function () { closeIt($this, datepicker); });
    						
    						jQuery("select[name=month]", datepicker).get(0).selectedIndex = chosendate.getMonth();
    						jQuery("select[name=year]", datepicker).get(0).selectedIndex = Math.max(0, chosendate.getFullYear() - opts.startyear);
    						loadMonth(null, $this, datepicker, chosendate);
    					}
    					
    				});
    			}
    
            });
    
        };
    
    	jQuery.fn.simpleDatepicker.formatOutput = function (dateObj) {
    		return  dateObj.getDate() + "/" + (dateObj.getMonth()+1) + "/" + dateObj.getFullYear();	
    	};
    	
    	jQuery.fn.simpleDatepicker.defaults = {
    		chosendate : today,
    		startdate : today.getFullYear(), 
    		enddate : today.getFullYear() + 1,
    		
    		x : 18, // must be in px
    		y : 18 // must be in px
    	};
    })(jQuery);
    Mi scuso per la lungaggine..

    Praticamente vorrei che, lo script mi interpretasse il primo valore come giorno, il secondo come mese ed il terzo come anno cosi da poterlo far impostare al calendario in apertura.
    Grazie mille...

  6. #6
    questo è il sito di riferimento:

    http://teddevito.com/demos/calendar.php

    grazie ancora

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.