Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7090949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:07:12+00:00 2026-05-28T08:07:12+00:00

I am validating text input on a html form, and would like to focus

  • 0

I am validating text input on a html form, and would like to focus and select the text if it is invalid.

If my validation function looks like (just an example):

jQuery.fn.DateHelper = function(text){
    return this.each(function(){
    //Make sure we're dealing with text-based form fields
    if(this.type != 'text')
    return;

    // call the processNumber function when the onBlur event is fired and there is a value in the field.
    $(this).blur(function() {
        if (this.value != '')                 
            processNUmber(this);
        });

    function processNumber( txtField ) {
        if (!isNan(txtField ))  {
            alert("not a number");
            this.focus(); // this does not work.
            // how to select text?     
        }

    }

};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:07:12+00:00Added an answer on May 28, 2026 at 8:07 am

    In your function you pass the txtField parameter, use it instead of this within the processNumber function:

    function processNumber( txtField ) {
        if (!isNan(txtField ))  {
            alert("not a number");
            txtField.focus();   
        }
    }
    

    Also you are declaring the processNumber function inside the .each() loop in the jQuery.fn.DateHelper function which doesn’t have to be the case, you can declare that function outside the .each() and it will be accessible to any element on which this plugin is run.

    UPDATE

    Your code functions however there are several syntactical errors:

    jQuery.fn.DateHelper = function(text){
        return this.each(function(){
            //Make sure we're dealing with text-based form fields
            if(this.type != 'text') return;
    
            // call the processNumber function when the onBlur event is fired and there is a value in the field.
            $(this).blur(function() {
                if (this.value != '') processNumber(this);//you were calling the processNUmber function, which doesn't exist
            }).focus(function () {
                this.select();
            });
    
            function processNumber( txtField ) {
                //isNan is broken: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/isNaN
                //also txtField is a DOM element, not a string
                txtField.value = txtField.value.replace(/[^0-9.]/g, "");
                if (txtField.value.length > 0)  {
                    alert("not a number");
                    txtField.focus();    
                }
            }
        });//this was omitted
    };
    

    Here is a demo: http://jsfiddle.net/uQ45E/6/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, in the following HTML ... <form name=outerform> <input type=text name=outer1/> <input type=text
I have a HTML script like this: <form id=no1> <input id=title type=text /> <input
This is my html form I am using. <label>Your Username:</label><input id=username name=username type=text onchange=return
I need to do a javascript form validation. I would like to validate each
I have text input boxes. There is validation for each of the boxes using
I'm validating a text field in my form as follows: $name = new Zend_Form_Element_Text('name');
I am looking at validation of some text boxes for things like required, minlength,
Among other text and visual aids on a form submission, post-validation, I'm coloring my
I have created an form validation using ajax/php. Each text box is validated using
<input type=text name=date value= dojoType=dijit.form.DateTextBox required=true /> I want to avoid dojoType for 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.