4 Diggs Spread This

Dynamic Inline JavaScript Form Validation

Posted Apr 30th, 2008 by Michael

Message Screenshot

This lightweight form alert script allows you to easily add attractive validation messages to your forms. There is no markup to add on your existing page. Just call the inlineMsg() function when you wish to display a message. The position of the form element (or any other element) is dynamically calculated.

Call the function as follows…

inlineMsg('name','You must enter your name.',2);

The first parameter is the id of the element you wish to display the warning in relation to. The second is the string you wish to display in the message box. You can include HTML, just be sure and escape when necessary. The third parameter is the time to display the message in seconds. It is optional and if left empty will default to the MSGHIDE variable in the JavaScript which is initially set to 3.

This has been tested working in IE6/IE7/IE8, Firefox, Opera and Safari. Please report any bugs to michael@leigeber.com.

Click here for the demo.

Click here to download the source.

Update 5/5/2008 - Updated the script to focus to the erroneous textbox when displaying an alert. Thanks to mbh for the suggestion.

17 Responses to “Dynamic Inline JavaScript Form Validation”

  • Ian Beadle on 01 May 2008 at 2:15 am

    Excellent work - very useful.
    Many thanks

  • Динамическая проверка данных с помощью Javascript | АяксЛайн.ру on 01 May 2008 at 5:30 pm

    [...] блог Майкла Лейгебера. Категория: JavaScriptАвтор: Spider Дата: 1 Май 2008 Время: 23:30 [...]

  • Fred on 02 May 2008 at 9:01 am

    The presentation looks very nice! I am having a slight issue, when I have a large form with the submit button at the bottom of say 2 pages, the script should maybe scroll the page up to the location of the error message.

    It should be a simple function to plug in to achieve this?
    window.scroll(x,y);

  • Michael on 02 May 2008 at 11:49 am

    @Fred - Absolutely, you could add in a window.scroll and position the y to the value of topposition plus some offset value. If you have any questions or need help just shoot me an email at michael@leigeber.com. Thanks for the feedback.

  • Fred on 02 May 2008 at 12:21 pm

    What I used was:

    window.scroll(0,topposition - 40);

    right after var leftposition in the function for inlineMsg, it’s working very nicely and looks great!

  • ЯR: webdev » Архив блога » Еще полезные ссылки on 04 May 2008 at 6:19 pm

    [...] Динамическая проверка форм с помощью Javascript [...]

  • mbh on 05 May 2008 at 6:41 am

    Hey Michael… just curious… in testing in Firefox (maybe it doesn’t for any browser) I noticed the focus doesn’t go to the particular field.

    If my test is correct… is there too much going (conflicting functions) or could the FOCUS be placed on the field in error without much trouble?

    Thanks

  • Michael on 05 May 2008 at 7:56 am

    @mbh - There must be a problem as the whole purpose of the function is for it to mark the erroneous field. Are you testing via the demo link or after you have integrated with your existing code? Shoot me a link via email and I will take a look. michael@leigeber.com

  • mbh on 05 May 2008 at 9:42 am

    Hmmmmmmmmmm…. I’m using FF 2.0.0.14… using the demo on your site. The message appears but the field focus doesn’t.

    First I thought the NOSCRIPT add-in might be blocking the focus.. but I allowed leigeber.com and it still didn’t quite do it’s FOCUS thing.

    You know how these things go… it might well be working for all other FF 2.0.0.14 users. I’ve tested 5 more times from different angles and no success yet. Shoot it could even be another add-in it’s conflicting with for some reason. I’ll keep trying as I love your smooth scripts.

  • mbh on 05 May 2008 at 9:46 am

    Hmmmmmm IE6 did not get me all the way across the street either. I’ll try it on some other systems as time permits!

  • Michael on 05 May 2008 at 10:05 am

    @mbh - I totally misunderstood you the first time through. I thought you were referring to the error message not not being next to the appropriate textbox, sorry about that. I will get the focus added in here in a few and repackage it for you to download. Thanks for the suggestion.

  • Michael on 05 May 2008 at 10:16 am

    @mbh - I have updated the script by adding line 65 “targetdiv.select();” which will select the content of the textbox with the error. Sorry again for the confusion.

  • mbh on 05 May 2008 at 12:46 pm

    Thanks so much!!! Got the stuff to work in FF and IE. Had trouble with the focus from my local machine… but I’ll track that as it indeed appears noscript is hanging that up from the local end!!!

    Appreciate the response/time. Smooth smooth scripts — I’ll have to keep an eye on your work. I’ve seen wayyyyyyyyyyyy too many scripts in my day… these seem to have that little extra “touch”, whatever it is.

    thanks…………..

  • Dynamic Inline JavaScript Form Validation on 06 May 2008 at 2:14 pm

    [...] JavaScript Form Validation - A lightweight form validation script that displays an attractive inline message that when there’s an error. There is no markup to add on your existing page. Just call the inlineMsg() function when you wish to display a message. Tested in IE6/IE7/IE8, Firefox, Opera and Safari. [...]

  • Mensajes dinámicos javascript en tus formularios | miguelpuig.com on 07 May 2008 at 4:20 am

    [...] JavaScript Form Validation es un script muy ligero que crea un pequeño tooltip a la hora de validar los campos del formulario. Es tan facil como llamar a la función inlineMsg() cuando quieras mostrar el mensaje. [...]

  • Laserolame : X-OR’s Blog » Blog Archive » un petit javascript à la rescousse pour vos formulaires on 07 May 2008 at 4:47 am

    [...] Le premier paramètre est l’id de l’élément sur lequel vous voulez afficher l’erreur, le deuxième paramère est le message à afficher et le troisième paramètre est le temps d’affichage de l’erreur en secondes. Site Web : http://www.leigeber.com/2008/04/dynamic-inline-javascript-form-validation/ [...]

Trackback URI | Comments RSS

Leave a Reply