• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

javascript regular expression related problem.

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,



I have created an HTML page.Which consists of two text fields.

I have used on keypress event to restrict user to enter only alphanumeric characters including space character in one text field and only numbers with only one dot(.) in other text field.

This is working fine when user enters values to those text fields using keyboard.

My problem is when user pastes data in those text fields special characters are also accepted which is not desirable.

I have used onPaste event and i am calling javascript function.Using regular expressions i am checking whether the pasted data is valid or not.

How to write that javascript function for onPaste event to validate two text fields,one to accept only alphanumeric characters including space and other to accept only numbers with only one dot(.) character.

Please help me out.
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please do not post the same question more than once. I have closed your duplicate.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you should be using onchange or onblue and not onpaste.

^[a-zA-Z\s]*$
^\d*\.\d*$

Eric
 
Author
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eric Pascarello:
you should be using onchange or onblue and not onpaste.

^[a-zA-Z\s]*$
^\d*\.\d*$

Eric



Actually if the first field goes alphanum, it would be ^[\w\s]*$.

Also, the thing with change/blur events is, they occur only once the user leaves the field, not when the value changes. There's no portable event for immediate change reaction, AFAIK.

A middle-of-the-road solution would be to periodically observe the field and strip whichever chars are invalid.

Something like this perhaps (untested):



Obviously, this should be namespaced and/or encapsulated properly, perhaps using Prototype's Field.Observer class�
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic