Forum Moderators: open

Message Too Old, No Replies

Can't get onfocusout to do what i want

         

SEOViking

10:09 am on Dec 6, 2005 (gmt 0)

10+ Year Member



I am trying to make a glocal form validation script for all of my forms in my web application, but I've realized that it is not a good idea to try. Therefore I am trying to break it down to individual input checks. I have three: checkLength, checkIllegalChars, checkNumOnly.

I have an input tag like this:

Text2 <input type="text" name="txtfld2" value="" onfocusout="checkLength(this,3);" />

and here we have the checkLength() javascript function:


function checkLength(thisField,maxLength) {
alert(thisField);
if (thisField.length > maxLength) {
alert("Feltet kan ha maks " + maxLength + " tall");
thisField.style.backgroundColor='#ff0000';
ready = false;
} else { ready = true; }
}

When the user puts the focus somewhere else (takes it out of that input) then I want that function to run and evaluate the input field. I can't get it to work in internet explorer or firefox. Any ideas? Am i attacking this the wrong way?

Here are my other functions:


function checkIllegalChars(thisField) {
// check illegalChars
var legalChars = /[^\w¦,¦\.¦\/¦@]/;
if (legalChars.test(thisForm.txtfld.value)) {
alert("\nTillatte tegn er\nA-Å (store og små bokstaver)\n0-9 (tall)\n, (komma)\n. (punktum)\n_ (underscore)\n/ (skråstrek)\n@ (alfakrøl)");
thisField.style.backgroundColor='#ff0000';
ready = false;
} else { ready = true; }
}


function checkNumOnly(thisField) {
// check numbersOnly
if (/[^0-9]/.test(thisField.value)) {
alert("Kun tall");
thisField.style.backgroundColor='#ff0000';
ready = false;
} else { ready = true; }
}

Span

10:43 am on Dec 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you need the "onblur" eventhandler instead of the MS "onfocusout".

Also, you don't want to check the field length, but the length of the field's value. So: "if (thisField.value.length > maxLength) {" should work better.

SEOViking

12:09 pm on Dec 6, 2005 (gmt 0)

10+ Year Member



Aaahhh, good observation about the value length =) Thanks. I'll give the onblur a try and post my results. Thanks again.

SEOViking

2:56 pm on Dec 6, 2005 (gmt 0)

10+ Year Member



onblur was the ticket! thanks span, you da man =)