The input box restricts the input to numbers

Yesterday, the blogger received a request saying that there are some input boxes in the form form. You have to limit it. Do n’t, for example, some restrictions can only allow him to enter numbers, not customers.

I provide you with two methods to achieve this function.Of course, I will explain the phenomenon to you here:

1. I believe that many front-end frameworks have integrated third-party component libraries. Here are some examples of iview used in my project.

iview has a numeric input text box:
<InputNumber :max="10" :min="1" v-model="value1"></InputNumber>

This is the easiest method, but in many cases, due to style and other factors, you cannot use the active component functions of the component library.Here I provide you with a second method

2. Regular expression judgment method

The second way is regular expression judgment:
oninput="this.value=this.value.replace(/^\D*([0-9]\d*\.?\d*)?.*$/,'$1')"

Note: The regular expression judgment method needs to use the native input input box, and the input box of iview or the input box of element should have no effect

The meaning of this regular expression is now the input of English and text

You can see the parameter oninput, I will explain to you:

Mouse events

  1. onclick: Event handler called when the user clicks on an object.
  2. oncontextmenu: triggered when the user clicks the right mouse button to open the context menu
  3. ondblclick: The event handler called when the user double-clicks an object.
  4. onmousedown: The mouse button was pressed.
  5. onmouseenter: triggered when the mouse pointer moves over the element
  6. onmouseleave: triggered when the mouse pointer moves out of the element
  7. onmousemove: The mouse is moved.
  8. onmouseover: Mouse over an element.
  9. onmouseout: The mouse moves away from an element.
  10. onmouseup: The mouse button is released.

Keyboard events

  1. onkeydown: A keyboard key was pressed.
  2. onkeypress: a keyboard key is pressed and released
  3. onkeyup: A keyboard key is released.

Frame / object events

  1. onabort: Image loading is interrupted. ()
  2. onbeforeunload: This event is triggered when the page is about to leave (refresh or close)
  3. onerror: An error occurred while loading the document or image. ( , with )
  4. onhashchange This event is triggered when the anchor part of the current URL is modified.
  5. onload A page or an image is loaded.
  6. onpageshow This event is triggered when the user visits the page
  7. onpagehide This event is triggered when the user leaves the current page and jumps to another page
  8. onresize The window or frame is resized.
  9. onscroll Event that occurs when the document is scrolled.
  10. onunload user exit page. ( with )

Form event

  1. onblur: triggered when the element loses focus
  2. onchange: This event is triggered when the content of the form element changes (,,, and)
  3. onfocus: triggered when the element gets focus
  4. onfocusin: triggered when the element is about to gain focus
  5. onfocusout: triggered when the element is about to lose focus
  6. oninput: triggered when the element gets user input
  7. onreset: triggered when the form is reset
  8. onsearch: triggered when the user enters text into the search field (<input = “search”>)
  9. onselect: triggered when the user selects text (and)
  10. onsubmit: triggered when the form is submitted

Clipboard events

  1. oncopy: This event is triggered when the user copies the content of the element
  2. oncut: This event is triggered when the user cuts the content of the element
  3. onpaste: This event is triggered when the user pastes the element content

Print events

  1. onafterprint: This event is triggered when the page has started printing, or the print window has been closed
  2. onbeforeprint: This event is triggered when the page is about to start printing

Drag event

  1. ondrag: This event is triggered when the element is being dragged
  2. ondragend: This event is triggered when the user finishes dragging the element
  3. ondragenter: This event is triggered when the dragged element enters the drop target
  4. ondragleave: This event is triggered when the drag element leaves the drop target
  5. ondragover: This event is triggered when the drag element is placed on the target
  6. ondragstart: This event is triggered when the user starts dragging an element
  7. ondrop: This event is triggered when the drag element is placed in the target area

Multimedia events

  1. onabort: The event is triggered when video / audio (audio / video) terminates loading.
  2. oncanplay: The event is triggered when the user can start playing video / audio (audio / video).
  3. oncanplaythrough: The event is triggered when audio / video can be played normally without pausing and buffering.
  4. ondurationchange: The event is triggered when the duration of audio / video changes.
  5. onemptied: triggered when the current playlist is empty
  6. onended: The event is triggered at the end of audio / video playback.
  7. onerror: Event is triggered when an error occurs during audio / video data loading.
  8. onloadeddata: The event is triggered when the browser loads the current frame of video / audio (audio / video).
  9. onloadedmetadata: The event is triggered after the metadata of the specified video / audio (audio / video) is loaded.
  10. onloadstart: The event is triggered when the browser starts looking for the specified video / audio (audio / video).
  11. onpause: The event is triggered when audio / video is paused.
  12. onplay: The event is triggered when the audio / video starts playing.
  13. onplaying: The event is triggered when the audio / video is paused or ready to restart playback after buffering.
  14. onprogress: The event is triggered when the browser downloads the specified video / audio (audio / video).
  15. onratechange: Event is triggered when the playback speed of audio / video changes.
  16. onseeked: The event is triggered after the user repositions the playback position of audio / video.
  17. onseeking: The event is triggered when the user starts to reposition audio / video.
  18. onstalled: Event is triggered when the browser gets media data, but the media data is not available.
  19. onsuspend: The event is triggered when the browser reading media data is suspended.
  20. ontimeupdate: The event is triggered when the current playback position sends a change.
  21. onvolumechange: The event is triggered when the volume changes.
  22. onwaiting: The event is triggered when the video needs to be buffered due to the next frame being played.

Animation event

  1. animationend: This event is triggered when the CSS animation ends
  2. animationiteration: This event is triggered when the CSS animation repeatedly plays
  3. animationstart: This event is triggered when the CSS animation starts to play

Transitional event

  1. transitionend: This event is triggered after the CSS completes the transition.

Other events

  1. onmessage: This event
    is triggered when a message is received through or from an object (WebSocket, Web Worker, Event Source or child frame or parent window)
  2. onmousewheel: Discontinued. Use onwheel event instead
  3. ononline: This event is triggered when the browser starts working online.
  4. onoffline: This event is triggered when the browser starts working offline.
  5. onpopstate: This event is triggered when the browsing history (history object) of the window changes.
  6. onshow: when the event The element is triggered when the context menu is displayed
  7. onstorage: This event is triggered when Web Storage (HTML 5 Web storage) is updated
  8. ontoggle: This event is triggered when the user opens or closes the element
  9. onwheel: This event is triggered when the mouse wheel scrolls up and down the element

I have listed all the events for everyone, I suggest that you still go to understand, the blogger just started using the onkeyup event, and later found that the mac can achieve the input input box to limit the input of numbers only, but on ordinary windows system computers , You can enter English if you enter the Chinese character and press Enter.After repeated tests, it is found that oninput meets the demand, but a little flashing effect will appear on the mac, which does not affect the operation!

Published 34 original articles · won praise 0 · Views 3634

Guess you like

Origin blog.csdn.net/qq_43469899/article/details/103627950