Skip to content

Instantly share code, notes, and snippets.

@JasonMillward
Last active December 22, 2015 08:29
Show Gist options
  • Save JasonMillward/6445232 to your computer and use it in GitHub Desktop.
Save JasonMillward/6445232 to your computer and use it in GitHub Desktop.
###
jQuery Auto Capitalise + Capslock checker
Adds CSS to input classes, and optionaly checks for capslock and promts to turn off
Should prevent ALL CAPS data from appearing in database
Released under the MIT license
Copyright (c) 2013, Jason Millward
@version $Id: 1, 2013-09-04 $;
@author Jason Millward <[email protected]>
@license http://opensource.org/licenses/MIT
###
$ = jQuery
debug = false
ucaseFirst = (str) ->
str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
log = (str) ->
if debug
currentTime = new Date
h = currentTime.getHours()
m = currentTime.getMinutes()
s = currentTime.getSeconds()
try
console.log("[" + h + ":" + m + ":" + s + "]\t" + str)
catch e
$.fn.extend
autoCap: (options) ->
settings =
message: "Please disengage capslock"
capslock: true
debug: false
settings = $.extend settings, options
debug = settings.debug
return @each ()->
input = $( this )
input.css "text-transform", "capitalize"
log "Preparing magic show on: " + input.attr("name")
input.bind "blur", ->
input.val( ucaseFirst input.val() )
if settings.capslock
input.bind "keypress", (e) ->
s = String.fromCharCode( e.which )
if s.toUpperCase() == s && s.toLowerCase() != s && !e.shiftKey
alert settings.message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment