Solved

validation using javascript

Posted on 2014-02-04
6
275 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
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
Comment Utility
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
Comment Utility
I need sample working code for the above two input fields using javascript
0
 
LVL 6

Accepted Solution

by:
Mahesh Bhutkar earned 500 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ksd123
Comment Utility
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
Comment Utility
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
Comment Utility
You are passing text1 element & not it's value.

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Image decoding from Camera 3 42
angularls and plnkr 14 15
Javascript generate change location 12 21
Different Delete Messages 7 4
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now