validation using javascript

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
ksd123Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Mahesh BhutkarConnect With a Mentor Commented:
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
 
Mahesh BhutkarCommented:
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
 
ksd123Author Commented:
I need sample working code for the above two input fields using javascript
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
ksd123Author Commented:
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
 
Mahesh BhutkarCommented:
You can validate using above regular expression ^([1-9][0-9]{0,8})*$ in javascript.

I already provided how to validate in javascript.
0
 
Mahesh BhutkarCommented:
You are passing text1 element & not it's value.

Change it to,
onclick="allnumericplusminus(document.form1.text1.value)"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.