In web development, forms are one of the important components of business logic, and form verification is a basic security consideration. In the form, the verification of the input box is the most basic and necessary, and judging whether the content of the input box is empty is the most common verification. Here we will introduce the method of javascript to realize real-time judgment that it is not empty.
- Real-time monitoring of the input box
In order to achieve the effect of real-time judgment that it is not empty, we need to monitor the input box in real time through javascript. We can update the text in the input box in real time by listening to the oninput event of the input.
<input>
In the above code, the oninput event is added to the input. When the event is triggered, a function named checkEmpty will be called. This function is used to determine whether there is a value in the input.
- Verify the value in the input box
After monitoring the input box in real time, we need to write a function checkEmpty to check whether there is a value in the input box, and change the input box according to whether there is a value style or prompt information.
function checkEmpty() { var inputEl = document.getElementById("input"); if (inputEl.value.trim() == "") { inputEl.style.borderColor = "red"; inputEl.nextElementSibling.innerHTML = "此项不能为空"; } else { inputEl.style.borderColor = "green"; inputEl.nextElementSibling.innerHTML = ""; } }
In the above code, we first obtain the DOM object of the input box, and then change the style or prompt information of the input box by determining whether the value of the input box is empty. When the input box is empty, we set the border color of the input box to red, and add the prompt message "This item cannot be empty" behind the input box; when the input box is not empty, set the border color to green , and clear the prompt information.
- Apply the validation function to all input boxes
Finally, we need to apply the checkEmpty function to all input boxes that require validation. In actual development, we often mark the input boxes that need to be verified with class, then use the document.getElementsByClassName() method to obtain all input boxes marked with this class, and then loop through and call the checkEmpty function again.
var inputs = document.getElementsByClassName("required"); for (var i = 0; i <p>In the above code, we first use the document.getElementsByClassName("required") method to obtain all input boxes that need to be verified, and then use loop traversal to add oninput events for all input boxes, and the events are triggered The checkEmpty function is executed for verification. </p><p>Summary: <br>Through the above three steps, we can implement a simple, javascript-based verification that the input box is not empty in real time. Of course, you may encounter more complex form validation requirements in actual development. You may wish to use the above example as a basis and make appropriate improvements based on business logic. </p>
The above is the detailed content of How to judge whether it is empty in real time with javascript. For more information, please follow other related articles on the PHP Chinese website!

React'slimitationsinclude:1)asteeplearningcurveduetoitsvastecosystem,2)SEOchallengeswithclient-siderendering,3)potentialperformanceissuesinlargeapplications,4)complexstatemanagementasappsgrow,and5)theneedtokeepupwithitsrapidevolution.Thesefactorsshou

Reactischallengingforbeginnersduetoitssteeplearningcurveandparadigmshifttocomponent-basedarchitecture.1)Startwithofficialdocumentationforasolidfoundation.2)UnderstandJSXandhowtoembedJavaScriptwithinit.3)Learntousefunctionalcomponentswithhooksforstate

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

JavaScriptfatigueinReactismanageablewithstrategieslikejust-in-timelearningandcuratedinformationsources.1)Learnwhatyouneedwhenyouneedit,focusingonprojectrelevance.2)FollowkeyblogsliketheofficialReactblogandengagewithcommunitieslikeReactifluxonDiscordt

TotestReactcomponentsusingtheuseStatehook,useJestandReactTestingLibrarytosimulateinteractionsandverifystatechangesintheUI.1)Renderthecomponentandcheckinitialstate.2)Simulateuserinteractionslikeclicksorformsubmissions.3)Verifytheupdatedstatereflectsin

KeysinReactarecrucialforoptimizingperformancebyaidinginefficientlistupdates.1)Usekeystoidentifyandtracklistelements.2)Avoidusingarrayindicesaskeystopreventperformanceissues.3)Choosestableidentifierslikeitem.idtomaintaincomponentstateandimproveperform

Reactkeysareuniqueidentifiersusedwhenrenderingliststoimprovereconciliationefficiency.1)TheyhelpReacttrackchangesinlistitems,2)usingstableanduniqueidentifierslikeitemIDsisrecommended,3)avoidusingarrayindicesaskeystopreventissueswithreordering,and4)ens

UniquekeysarecrucialinReactforoptimizingrenderingandmaintainingcomponentstateintegrity.1)Useanaturaluniqueidentifierfromyourdataifavailable.2)Ifnonaturalidentifierexists,generateauniquekeyusingalibrarylikeuuid.3)Avoidusingarrayindicesaskeys,especiall


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
