Skip to content

Instantly share code, notes, and snippets.

View csharpforevermore's full-sized avatar
💭
Developing

Randle csharpforevermore

💭
Developing
View GitHub Profile
@csharpforevermore
csharpforevermore / Pick first option for a select element
Created July 24, 2013 10:16
Select the first child item of a select element
$('select').val($('select>option:nth-child(1)').val());
@csharpforevermore
csharpforevermore / Find clicked LI element
Created July 24, 2013 10:17
In a HTML unordered list, get the list item that was clicked (UL > LI) Code copied from http://stackoverflow.com/questions/3545341/jquery-get-the-id-value-of-li-after-click-function
$("ul.nav li").click(function () {
alert(this.id); // id of clicked li by directly accessing DOMElement property
alert($(this).attr('id')); // jQuery's .attr() method, same but more verbose
alert($(this).html()); // gets innerHTML of clicked li
alert($(this).text()); // gets text contents of clicked li
});
@csharpforevermore
csharpforevermore / Bootstrap - allow other element to trigger click of button for drop down menu
Created July 24, 2013 10:19
Using Twitter Bootstrap library - it is sometimes necessary to trigger a drop down control on another element (e.g. button). The span "pseudo-toggle" wraps the textbox "title" that will contain the selected title value (Mr or Mrs). Normally the user is expected to click on the button "title-button" to select the drop down. Now they can click any…
<div class="controls">
<div class="input-append">
<!-- gender -->
<span id="pseudo-toggle"><input name="title" id="title" type="text" required="" disabled="disabled" /></span>
<div class="btn-group">
<button id="title-button" class="btn dropdown-toggle" data-toggle="dropdown">
<img src="images/mobile/btn-dropdown.gif" alt="" /></button>
<ul class="dropdown-menu">
<li><a href="#">Mr</a></li>
<li><a href="#">Mrs</a></li>
@csharpforevermore
csharpforevermore / User can only enter digits in input text
Created July 24, 2013 10:21
Restrict the user to only being able to type a digit
$('input').bind('keypress', function (event) {
var regex = new RegExp("[\d]");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});
$('input').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z0-9]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});
@csharpforevermore
csharpforevermore / gist:6070228
Created July 24, 2013 12:44
Mock the HttpContext object
var httpContext = new HttpContext(new HttpRequest("", url ?? "http://test", ""), new HttpResponse(new StringWriter()));
if (!userName.IsNullOrWhiteSpace())
{
var principal = Substitute.For<IPrincipal>();
principal.Identity.Name.Returns(userName);
httpContext.User = principal;
}
@csharpforevermore
csharpforevermore / Insert jQuery into any site via (Chrome) console
Created July 30, 2013 16:18
In those cases where you want to use jQuery on a site, to which you possibly do not have the source code. Source: http://stackoverflow.com/questions/7474354/include-jquery-in-the-javascript-console
var jq = document.createElement('script');
jq.src = "http://code.jquery.com/jquery-latest.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
jQuery.noConflict();
@csharpforevermore
csharpforevermore / Umbraco Razor - find page by document type alias not ID
Created July 30, 2013 16:25
If you want to find the ID of the first node with a specific Document Type, use this code by supplying the document type alias here (i.e. the method signature property "docTypeAlias").
public int GetFirstNodeWithDocumentAlias(string docTypeAlias)
{
int locationsPageId = -1;
DynamicNode searchPage = null;
var nodes = new DynamicNode(-1).Descendants();
foreach (DynamicNode node in nodes){
if (node.NodeTypeAlias.ToLower().StartsWith(docTypeAlias.ToLower()))
{
searchPage = node; break;
}
@csharpforevermore
csharpforevermore / Console.log
Created August 7, 2013 09:55
Write text to the browser's console - if it has one - whilst ensuring we avoid generating an exception (i.e. in IE)
if (window.console && console.log) {
console.log('Hello world!');
}
@csharpforevermore
csharpforevermore / Email(property).cs
Created August 7, 2013 15:59
In ASP.NET MVC 4, using the Entity Framework (code first) approach, I needed to create a RegEx for emails - allowing uppercase or lowercase. This was the solution for me.
[Required]
[RegularExpression(@"\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b", ErrorMessage = "Invalid email address")]
[DisplayName("E-mail addresss")]
public string Email { get; set; }