?
Solved

validation using javascript

Posted on 2014-02-04
6
Medium Priority
?
289 Views
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
0
Comment
Question by:ksd123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

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

Here is an good example,
http://www.regular-expressions.info/javascriptexample.html

For Good basic knowledge,
http://www.w3schools.com/js/js_obj_regexp.asp

For Regular expressions detailings,
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp
0
 

Author Comment

by:ksd123
ID: 39832075
I need sample working code for the above two input fields using javascript
0
 
LVL 6

Accepted Solution

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

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

      //Test match
      if(strValue.match(rematch))
            return true;
      else
            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 );
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ksd123
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
0
 
LVL 6

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.
0
 
LVL 6

Expert Comment

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

Change it to,
onclick="allnumericplusminus(document.form1.text1.value)"
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

770 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