Javascript validation onkeypress function - Stack Overflow

In web form, I have multiple fields and each field have some unique validation like phone and zip code

In web form, I have multiple fields and each field have some unique validation like phone and zip code have only number and some of the fields do not allow special characters. So how do I validate each field?

Now I validating like

function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 32 && (charCode < 48 || charCode > 57)||(charCode==32))

        return false;

        return true;
      } 

function isAlphaNumericKey(evt)
    {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 32 && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)&& (charCode < 48 || charCode > 57))
    return false;
    return true;
    }  

and some cases I need to allow some special characters like

function isANhypenKey(evt)
    {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 32 && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)&& (charCode < 48 || charCode > 57) && (charCode!=44) && (charCode!=45))
    return false;
    return true;
    }  

and HTML form

<input type="text"  name="zipcode" id="zipcode" onkeypress="return isNumberKey(event);"/>
<input type="text"  name="shipname"  id="shipname"  onkeypress="return isANhypenKey(event);"  />

How to reduce my JS code. Can I get any JS library file for this or JS function can solve this? Thanks

In web form, I have multiple fields and each field have some unique validation like phone and zip code have only number and some of the fields do not allow special characters. So how do I validate each field?

Now I validating like

function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 32 && (charCode < 48 || charCode > 57)||(charCode==32))

        return false;

        return true;
      } 

function isAlphaNumericKey(evt)
    {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 32 && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)&& (charCode < 48 || charCode > 57))
    return false;
    return true;
    }  

and some cases I need to allow some special characters like

function isANhypenKey(evt)
    {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 32 && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)&& (charCode < 48 || charCode > 57) && (charCode!=44) && (charCode!=45))
    return false;
    return true;
    }  

and HTML form

<input type="text"  name="zipcode" id="zipcode" onkeypress="return isNumberKey(event);"/>
<input type="text"  name="shipname"  id="shipname"  onkeypress="return isANhypenKey(event);"  />

How to reduce my JS code. Can I get any JS library file for this or JS function can solve this? Thanks

Share Improve this question asked Aug 8, 2014 at 10:14 PNGPNG 2853 gold badges7 silver badges19 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

There are a couple of libraries that you could use. If you want to stick to pure JavaScript without any jQuery, then your best option would probably be Validate JS.

There are a ton of jQuery options if you are willing to work with jQuery - these are usually more feature packed and nicer to look at too. You could also use the Validator built into the Foundation Framework - it's called Abide but it uses jQuery.

Hope this helps.

This may or may not be the answer you are looking for, but perhaps you should be looking at a solution that requires less JavaScript:

In HTML 5, you can specify the type of value that an input is supposed to accept using a pattern, you can read about this on this mozilla page or by reading the answers on this question: HTML5 Form Pattern / Validation.

<input type="text" name="country_code" pattern="put a regex here that describes only valid input for your situations" title="Three letter country code">

Note that not all browsers (primarily Safari and older IE) currently support the pattern attribute.

Another thing of note is that it may be preferable to use a RegEx in your JavaScript code, should that be the preferred solution.

yeah http://rickharrison.github.io/validate.js/ best and simple to use check that link

You may take a look to the jQuery validation plugin - it's very useful, you also easily can add validation errors to the form so user will know what is wrong with it's input.

In this way you will avoid a lot of manual JS-validation code.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745039510a4607727.html

相关推荐

  • Javascript validation onkeypress function - Stack Overflow

    In web form, I have multiple fields and each field have some unique validation like phone and zip code

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信