[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

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
0
ksd123
Asked:
ksd123
  • 4
  • 2
1 Solution
 
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
 
Mahesh BhutkarCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now