Javascript events: numeric textbox, tab key in a textarea

I've played around with JavaScript key press events quite a bit over the years.

The most comprehensive script I wrote around this subject, is a mask control which we use on our intranet for all kinds of things (Code which I unfortunately can't share with you, else I might get shot or something).

Just to get the ball on the roll (get the basics in place), lets imagine for a moment that we want to hinder users to type anything but numeric values into a textbox (obviously one would still need to have server side checks in place)

 
function numeric(e) 
{
    return ((e.keyCode == 8) || (e.keyCode > 47 && e.keyCode <58)); 
}
 

All we need to do from here is attach the function to a textbox, which will prevent the user from entering anything but numeric values.

 
<input type="text" name="somename" onkeydown="return numeric(event)" />
 

Taking it a bit further, let's say we've got a textarea wherein we want to modify the way it behaves to the tab key - normally pressing the tab key would take us to the next control on the page.

We would however like the user to have a more natural experience and provide them with the ability to use the tab key within a textarea. (Something that would be quite nice in a web application like phpmyadmin)

Have a look at the following code (works in IE and Firefox)

 
function allowTab(sender, e) 
{
	if (e.keyCode == 9)
	{
		if (e.srcElement)
		{
			sender.selection = document.selection.createRange();
			sender.selection.text = "\t";
		}
		else if (e.target)
		{
			var start = sender.value.substring(0, sender.selectionStart);
			var end = sender.value.substring(sender.selectionEnd, sender.value.length);						
			var newRange = sender.selectionEnd + 1;
			var scrollPos = sender.scrollTop;
			sender.value = String.concat(start, "\t", end);					
			sender.setSelectionRange(newRange, newRange);
			sender.scrollTop = scrollPos;
		}
		return false;
	}
	else
	{
		return true;
	}
}
 

We pass the current object and its event to the function, by attacthing it to the onkeydown event, like below.

 
<textarea name="somename" style="width:600px; height:600px" onkeydown="return allowTab(this, event)">
</textarea>
 




Post/View comments
 

Office automation: Converting doc to docx

With the advent of Office 2007, Microsoft switched over to its OpenXML standard for office documents - which is quite a subject in itself, one which I will blog about sometime in the future.

This post however, is about converting older word documents to this new format. I've seen a few sites that actually offer a conversion service (for a fee) - wonder if that's even legal, seeing as Microsoft provides a free tool (ofc.exe) as part of its migration planning manager, which is available from this link.

It's a rather funny utility, which works in conjunction with the Office 2007 compatibility pack.

The compatibility pack mainly enables us to open OpenXML documents in older versions of Office; minus all the new functionality in Office 2007. Click here

Getting back to the ofc tool, you will notice a file called ofc.ini; this file contains a number of settings which you will need to set. Most notably the following highlighted options.

 
[ConversionOptions] section.
[ConversionOptions]
; FullUpgradeOnOpen: if set to 1, Word documents will be fully converted to the OpenXML format
;                    if set to 0 (default), Word documents will be saved in the OpenXML format in compatibility mode
; Not applicable to Excel or PowerPoint files.
FullUpgradeOnOpen=0
 
[FoldersToConvert]
; The Converter will attempt to convert all supported files in the specified folders
; (do not include if specifying FileListFolder)
;fldr=C:\Documents and Settings\Administrator\My Documents
fldr=c:\abc
 

We can alternatively do this programmatically using the Office 2007 Interop assemblies, available here if we want to do a bit more than merely convert it to new standards.

In this example, we're simply going to convert a folder containing older documents, to the new docx:

 
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
using System.IO;
class Program
{
    static void Main(string[] args)
    {
        Word._Application application = new Word.Application();
        object missing = Missing.Value;
        object fileformat = Word.WdSaveFormat.wdFormatXMLDocument;
        DirectoryInfo directory = new DirectoryInfo(@"c:\abc");
        foreach (FileInfo file in directory.GetFiles("*.doc", SearchOption.AllDirectories))
        {
            if (file.Extension.ToLower() == ".doc")
            {
                object filename = file.FullName;
                object newfilename = file.FullName.ToLower().Replace(".doc", ".docx");
                Word._Document document = application.Documents.Open(ref filename,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing);
                document.Convert();
                document.SaveAs(ref newfilename, ref fileformat, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing);
                document.Close(ref missing, ref missing, ref missing);
                document = null;
            }
        }
        application.Quit(ref missing, ref missing, ref missing);
        application = null;
    }
}
 

Notice "document.Convert()", this method tells the interop assembly that the documents need to be fully converted to the new OpenXML format - something you might want to omit if you're planning to provide support for previous versions of office using the compatibility pack.




Post/View comments
 
First 21 22 23 24 25 26 27 28 29 30 Last / 42 Pages (83 Entries)