validation using javascript

Posted on 2014-02-04
Last Modified: 2014-02-19
Hi Experts,

I  need  to validate input fields using javascript  and  want sample code (using functions)  for the following fields in javascript so that I can use these functions in other pages too.

1)Validate numbers only

a)should not allow alphabets or special characters
b)should not start with Zero
c)maximum numbers allowed is 9

Valid Entries: 11111,123,100000000,1 etc

Invalid Entries:0111(started with zero),1234567891(10 digits) etc

2)Validate Name

a)Should not contain special characters

valid Entries:abc def,ABC DEF, ddsfas dfasdfa etc
Invalid Entries: 12ab asd (contain digits), ##$@ dfasdf (contain special characters) etc
Question by:ksd123
  • 4
  • 2

Expert Comment

by:Mahesh Bhutkar
ID: 39832039
You can use regular expressions in your javascript to achieve this.

Here is an good example,

For Good basic knowledge,

For Regular expressions detailings,

Author Comment

ID: 39832075
I need sample working code for the above two input fields using javascript

Accepted Solution

Mahesh Bhutkar earned 500 total points
ID: 39832096
You can use common function for both validation,

function MatchClick(strValue, strMatchPattern) {
      var rematch = new RegExp(strMatchPattern);

      //Test match
            return true;
            return false;

Where strMatchPattern will be pattern/regex for each validation which you have to pass as follows,

//To Validate Name
var alphanum = "^([a-zA-Z0-9 ])*$";

//To Validte Number
var nummax9 =  "^([1-9][0-9]{0,8})*$";

//Invoke MatchClick function
MatchClick("100000000", alphanum);
MatchClick("ddsfas dfasdfa", nummax9 );
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.


Author Comment

ID: 39835254
Hi Experts,

I am trying to validate below conditions using javascript  and having some issues. can anyone correct the errors. Here is the url validate number

1)Validate numbers only

a)should not allow alphabets or special characters
b)should not start with Zero
c)maximum numbers allowed is 9

Expert Comment

by:Mahesh Bhutkar
ID: 39835297
You can validate using above regular expression ^([1-9][0-9]{0,8})*$ in javascript.

I already provided how to validate in javascript.

Expert Comment

by:Mahesh Bhutkar
ID: 39835306
You are passing text1 element & not it's value.

Change it to,

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hide un-named HTML Label with CSS 14 36
Need to combine two scripts 2 33
Binding a Grid in Javascript 5 42
Javascript 2 17
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question