This is not CF but it sure comes in handy.
If for any reason you need to get rid pesky trailing characters, here's the script:
<script>
function trimTrailingChars(s, charToTrim)
{
var regExp = new RegExp(charToTrim + "+$");
var result = s.replace(regExp, "");
return result;
}
</script>
And here is how you use it in your code:
var sText = trimTrailingChars(sText, ',');
Note the red text above. Change the comma to whatever you want to get rid of.
Tuesday, October 6, 2015
Friday, March 13, 2015
Customized placement of jQuery error messages
The code is pretty simple. I have highlighted the most important parts in red.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src='//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script>
<script src='https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js'></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#DateChooser" ).datepicker();
});
</script>
<style class="cp-pen-styles">
.error {font-size:12px; color:#CC0000;}
</style>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function(){
$("#form").validate({
messages: {
name: "Please enter name.",
surname: "Please enter surname.",
DateChooser: "Please enter date."
},
errorElement: "div",
errorPlacement: function(error, element) {
error.insertAfter(element);
}
});
});
});//]]>
</script>
</head>
<body>
<cfform id="form">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="required" minlength="2" />
<br/>
<div></div>
<br>
<label for="DateChooser">Date</label>
<input id="DateChooser" class="required" name="DateChooser">
<br/>
<div></div>
<br>
<label for="surname">Surname</label>
<input type="text" name="surname" id="surname" class="required" minlength="2" />
<br />
<div></div>
<input type="submit" value="submit" id="submit" />
</cfform>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src='//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script>
<script src='https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js'></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#DateChooser" ).datepicker();
});
</script>
<style class="cp-pen-styles">
.error {font-size:12px; color:#CC0000;}
</style>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function(){
$("#form").validate({
messages: {
name: "Please enter name.",
surname: "Please enter surname.",
DateChooser: "Please enter date."
},
errorElement: "div",
errorPlacement: function(error, element) {
error.insertAfter(element);
}
});
});
});//]]>
</script>
</head>
<body>
<cfform id="form">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="required" minlength="2" />
<br/>
<div></div>
<br>
<label for="DateChooser">Date</label>
<input id="DateChooser" class="required" name="DateChooser">
<br/>
<div></div>
<br>
<label for="surname">Surname</label>
<input type="text" name="surname" id="surname" class="required" minlength="2" />
<br />
<div></div>
<input type="submit" value="submit" id="submit" />
</cfform>
</body>
</html>
Thursday, March 12, 2015
Easy jQuery Error Message Customization
This code will allow you to customize your jQuery error messages:
Add these links to your page
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src='//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script>
<script src='https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js'></script>
You can style your error messages:
<style class="cp-pen-styles">
.error {
color:red;
font-family:verdana, Helvetica;
}
</style>
Your script
<script>
$(function () {
$('#formTest').validate({
rules: {
name: { required: true },
address: { required: true }
},
messages: {
name: { required: 'Please enter your name' },
address: { required: 'Please enter your address' }
}
});
});
</script>
Your form
<form action='test.cfm' id="formTest" name="formTest" method='POST'>
<input name="name" value="" />
<input name="address" value="" />
<input type="submit" class="submit" value="Add Appeal Info">
</form>
Add these links to your page
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src='//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script>
<script src='https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js'></script>
You can style your error messages:
<style class="cp-pen-styles">
.error {
color:red;
font-family:verdana, Helvetica;
}
</style>
Your script
<script>
$(function () {
$('#formTest').validate({
rules: {
name: { required: true },
address: { required: true }
},
messages: {
name: { required: 'Please enter your name' },
address: { required: 'Please enter your address' }
}
});
});
</script>
Your form
<form action='test.cfm' id="formTest" name="formTest" method='POST'>
<input name="name" value="" />
<input name="address" value="" />
<input type="submit" class="submit" value="Add Appeal Info">
</form>
Thursday, January 29, 2015
How to find spaces in a form field in Coldfusion
This is pretty straight forward
<cfset strTest = "#form.variable#">
<cfset strTarget = " ">
<cfset newTest=Chr(1) & strTest & Chr(1)>
<cfset intCount=DecrementValue(ArrayLen(newTest.split(strTarget.replaceAll("(\W)","\\$1"))))>
<cfoutput>#intCount#</cfoutput>
This will count the number of spaces in the variable.
If the intCount = 0, you are good to go.
You can use the code to test for any character.
Enjoy!
<cfset strTest = "#form.variable#">
<cfset strTarget = " ">
<cfset newTest=Chr(1) & strTest & Chr(1)>
<cfset intCount=DecrementValue(ArrayLen(newTest.split(strTarget.replaceAll("(\W)","\\$1"))))>
<cfoutput>#intCount#</cfoutput>
This will count the number of spaces in the variable.
If the intCount = 0, you are good to go.
You can use the code to test for any character.
Enjoy!
Thursday, December 18, 2014
How to dynamically define quarterly dates in Coldfusion
You know those reports that upper management request from time to time? The ones that give you nightmares? Yeah those. Well sometimes they will request quarterly reports so I wanted to share an easy way to define quarterly dates.
So we start with our default variables:
<cfset quarter = 1>
<cfset year = "#year(now())#">
<cfset startmonth = 1>
And here are the functions that create the dates:
<cffunction name="QuarterFirstDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="#year(now())#">
<cfargument name="startmonth" type="numeric" default="1">
<cfset firstDate = DateAdd("m",startmonth-1,CreateDate(yr, ((quarternumber-1)*3)+1, "1"))>
<cfreturn firstDate>
</cffunction>
<cffunction name="QuarterLastDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="#year(now())#">
<cfargument name="startmonth" type="numeric" default="1">
<cfset lastDate = DateAdd("m",startmonth-1,CreateDate(yr, quarternumber*3, DaysInMonth(CreateDate(yr, quarternumber*3, "1"))))>
<cfreturn lastDate>
</cffunction>
And here is how I define my returned variables to be used in my code:
<cfset startDate = #DateFormat(QuarterFirstDate(quarter, year, startmonth))#>
<cfset endDate = #DateFormat(QuarterLastDate(quarter, year, startmonth))#>
To change the quarters, simply change the default quarter variable define at the top of the page:
<cfset quarter = 1> = 01-Jan-14 - 31-Mar-14
<cfset quarter = 2> = 01-Apr-14 - 30-Jun-14
<cfset quarter = 3> = 01-Jul-14 - 30-Sep-14
<cfset quarter = 4> 01-Oct-14 - 31-Dec-14
nJoy!
So we start with our default variables:
<cfset quarter = 1>
<cfset year = "#year(now())#">
<cfset startmonth = 1>
And here are the functions that create the dates:
<cffunction name="QuarterFirstDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="#year(now())#">
<cfargument name="startmonth" type="numeric" default="1">
<cfset firstDate = DateAdd("m",startmonth-1,CreateDate(yr, ((quarternumber-1)*3)+1, "1"))>
<cfreturn firstDate>
</cffunction>
<cffunction name="QuarterLastDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="#year(now())#">
<cfargument name="startmonth" type="numeric" default="1">
<cfset lastDate = DateAdd("m",startmonth-1,CreateDate(yr, quarternumber*3, DaysInMonth(CreateDate(yr, quarternumber*3, "1"))))>
<cfreturn lastDate>
</cffunction>
And here is how I define my returned variables to be used in my code:
<cfset startDate = #DateFormat(QuarterFirstDate(quarter, year, startmonth))#>
<cfset endDate = #DateFormat(QuarterLastDate(quarter, year, startmonth))#>
To change the quarters, simply change the default quarter variable define at the top of the page:
<cfset quarter = 1> = 01-Jan-14 - 31-Mar-14
<cfset quarter = 2> = 01-Apr-14 - 30-Jun-14
<cfset quarter = 3> = 01-Jul-14 - 30-Sep-14
<cfset quarter = 4> 01-Oct-14 - 31-Dec-14
nJoy!
Wednesday, July 9, 2014
How to return a set number of rows in SQL using a variable
You're gonna love me for this one :)
I'll make it very simple too.
So I am sure you've use the TOP clause to retreive a certain number of rows from a query, right?
Here is an example of how you can set the value of TOP as a variable based on, let's say...user input.
CREATE PROCEDURE [dbo].[testRowCount]
(
@total int=0 input variable. This can be based on anything like user input, etc.
)
AS
DECLARE @top INT what you will use as the TOP variable
SET @top = @total here you set the value for TOP based on the input
And here is how you use in in the select statement
SELECT TOP(@top) *
FROM table
WHERE....
You're welcome :)
I'll make it very simple too.
So I am sure you've use the TOP clause to retreive a certain number of rows from a query, right?
Here is an example of how you can set the value of TOP as a variable based on, let's say...user input.
CREATE PROCEDURE [dbo].[testRowCount]
(
@total int=0 input variable. This can be based on anything like user input, etc.
)
AS
DECLARE @top INT what you will use as the TOP variable
SET @top = @total here you set the value for TOP based on the input
And here is how you use in in the select statement
SELECT TOP(@top) *
FROM table
WHERE....
You're welcome :)
Tuesday, June 10, 2014
Stored Proc Clean Up - Getting rid of procs you don't use
If you are like me, then you HATE clutter. I am a minimalist - yep even when coding. I hate to see stored procs laying around that I:
1. Don't know what they do OR
2. Don't know if they are even in use
Of course, you don't want to start deleting procs randomly (that's a no-no)
So here are a few snippets to help you with your SQL house keeping:
This one tells you all of the procs that have run since the last restart.
SELECT p.name, last_execution_time, s.type_desc, create_date, modify_date
FROM sys.dm_exec_procedure_stats AS s
INNER JOIN sys.procedures AS p
ON s.[object_id] = p.[object_id]
ORDER BY p.name;
And this one shows procs that have NOT been run since last restart:
SELECT p.*
FROM sys.procedures AS p
LEFT JOIN sys.dm_exec_procedure_stats AS s
ON s.[object_id] = p.[object_id]
WHERE s.object_id IS null
order by name
Happy Cleaning!
1. Don't know what they do OR
2. Don't know if they are even in use
Of course, you don't want to start deleting procs randomly (that's a no-no)
So here are a few snippets to help you with your SQL house keeping:
This one tells you all of the procs that have run since the last restart.
SELECT p.name, last_execution_time, s.type_desc, create_date, modify_date
FROM sys.dm_exec_procedure_stats AS s
INNER JOIN sys.procedures AS p
ON s.[object_id] = p.[object_id]
ORDER BY p.name;
And this one shows procs that have NOT been run since last restart:
SELECT p.*
FROM sys.procedures AS p
LEFT JOIN sys.dm_exec_procedure_stats AS s
ON s.[object_id] = p.[object_id]
WHERE s.object_id IS null
order by name
Happy Cleaning!
Subscribe to:
Posts (Atom)