MediaWiki:Gadget-UploadValidation.js: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
(Adjusted according to rename to Upload Validation and added 'Temporary upload-related things' from Common.js (the Required attribute for Source))
m (Minor code formatting tweaks)
Line 12: Line 12:
//Add space preceding text in Desc and Source
//Add space preceding text in Desc and Source
$("#mw-input-source, #wpUploadDescription").each(function() {
$('#mw-input-source, #wpUploadDescription').each(function() {
var valFirstChar = $(this).val().charAt(0);
var valFirstChar = $(this).val().charAt(0);
if (valFirstChar !== "" && valFirstChar !== " ")
if (valFirstChar !== '' && valFirstChar !== ' ')
$(this).val( " " + $(this).val() );
$(this).val( ' ' + $(this).val() );
});
});
//Make file extension lowercase
//Make file extension lowercase
var fileName = $('#wpDestFile').val()
var fileName = $('#wpDestFile').val()
  , ext = fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();
  , ext = fileName.substring(fileName.lastIndexOf('.')+1).toLowerCase();
$('#wpDestFile').val( fileName.slice(0, -1*ext.length) + ext );
$('#wpDestFile').val( fileName.slice(0, -1*ext.length) + ext );
Line 26: Line 26:
//Add the html5 'required' attribute to the Source field on Special:Upload
//Add the html5 'required' attribute to the Source field on Special:Upload
$("#mw-input-source").prop('required',true);
$('#mw-input-source').prop('required', true);
//Add helpful link to Help:Upload#Source
//Add helpful link to Help:Upload#Source
$("label[for=mw-input-source]").html('Source (<a href="/Help:Upload#Source" target="_blank" title="Help:Upload">help</a>)');
$('label[for=mw-input-source]').html('Source (<a href="/Help:Upload#Source" target="_blank" title="Help:Upload">help</a>)');
}
}
if ( mw.config.get("wgPageName") === "Special:Upload" ) {
if ( mw.config.get('wgPageName') === 'Special:Upload' ) {
$( upload_validation() );
$( upload_validation() );
}
}

Revision as of 13:56, 17 July 2017

// --------------------------------------------------
// Upload Validation
// Includes:
//   Force lowercase file extensions - This ensures uploaded files have their extension in lowercase
//   Make the Source required
//   Add a space before Desc and Source for after the equals signs in the output template (temp)
// --------------------------------------------------

function upload_validation() {
	
	$('#mw-upload-form').on('submit', function(){
		
		//Add space preceding text in Desc and Source
		$('#mw-input-source, #wpUploadDescription').each(function() {
			var valFirstChar = $(this).val().charAt(0);
			if (valFirstChar !== '' && valFirstChar !== ' ')
				$(this).val( ' ' + $(this).val() );
		});
		
		//Make file extension lowercase
		var fileName = $('#wpDestFile').val()
		  , ext = fileName.substring(fileName.lastIndexOf('.')+1).toLowerCase();
		$('#wpDestFile').val( fileName.slice(0, -1*ext.length) + ext );
		
	});
	
	//Add the html5 'required' attribute to the Source field on Special:Upload
	$('#mw-input-source').prop('required', true);
	//Add helpful link to Help:Upload#Source
	$('label[for=mw-input-source]').html('Source (<a href="/Help:Upload#Source" target="_blank" title="Help:Upload">help</a>)');
	
}
if ( mw.config.get('wgPageName') === 'Special:Upload' ) {
	$( upload_validation() );
}