Coding Tips (JavaScript/CSS/VBA/Win32)

Useful code snippets, tips and some Windows applications
Use ProxyChanger 2.2
to change IE proxy settings

Javascript Tips

How To Prevent The Enter Button From Submitting The Form in Internet Explorer

If you have a form with one text box in it, normally, pressing the Enter button will submit the form. What if you have a custom Javascript function that is supposed to submit the form only if the text box entry is validated. This function would be called on the submit event. For example, onSubmit='Validate()'. This would work fine if the user clicks on the 'Submit' button. However, pressing the Enter button would process the validation function and then proceed directly to submitting the form.
What is the solution?
Internet search would reveal some solutions that involve capturing the keystroke, and doing some tricks when keystroke equals 13.
However, a simple solution would be to make the validating function return true or false depending on the result of validation, and then use the following code on the submit event : onSubmit='return Validate()'. And that's it.

If an opposite task is required, that is force the enter key to submit a form, the following code may be used:

document.onkeypress =
  function (evt) {
    var c = document.layers ? evt.which
            : document.all ? event.keyCode
            : evt.keyCode;
    if(c==13){
    	document.form1.submit();
    }
    return true;
  };

How To Keep The Focus On An Invalid Field

Have the validating function return true or false depending on the validation result. Then trigger the validation function by the Change event in a textbox,like this : onChange='return fnValidate(this)' . An example of the validating function code:
if (document.form1.text1.value == "")
    {
        window.alert("Please enter a valid bla-bla-bla.");
        document.form1.text1.focus();
        return false;
    }
    
An unwanted side effect is that the user won't be able to click on any other fields in the form until she corrects an invalid entry.

Make a text-box read-only

Use the blur() function in the text-box: onFocus='blur()'
Try it:

Function to get a file name from a path

function findName(str){
	pos = 0;
	while(pos>-1){
		pos = str.indexOf("\\");
		str  = str.substr(pos+1);
	}
	return str;
}

More Tips

Disable Right Click on Images

Validation

Very useful JavaScript validation routines by Karen Gayda
A MSDN article on validation

Links

Sorting HTML Tables using Javascript - Excellent !
An extensive JavaScript Form FAQ Knowledge Base from It.org