आइए एक उदाहरण देखें जहां हम onsubmit पर इनपुट टेक्स्ट को वैलिडेट कर रहे हैं:
उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <form onsubmit="return validateTheForm()" method="GET" action="https://mail.google.com/mail/"> <br/> <input type="text" id="txtInput" value=""/> <input type="submit" value="search"/> </form> <script> function validateTheForm(){ var validation = (document.getElementById('txtInput').value == 'gmail'); if(!validation){ alert('Something went wrong...Plese write gmail intext box and click'); return false; } return true; } </script> </body> </html>
उपरोक्त प्रोग्राम चलाने के लिए, फाइल का नाम "anyName.html(index.html)" सेव करें और फाइल पर राइट-क्लिक करें। VS कोड एडिटर में "Open with Live Server" विकल्प चुनें।
आउटपुट
यह निम्न आउटपुट उत्पन्न करेगा:

चरण 1
टेक्स्ट बॉक्स में GMAIL टाइप करें:

सर्च बटन पर क्लिक करने के बाद, आउटपुट निम्न है:

चरण 2
GMAIL के अलावा कोई अन्य वैल्यू टाइप करने पर, आपको अलर्ट मैसेज मिलेगा।
पहले वैल्यू टाइप करें और एंटर दबाएं:

सर्च बटन पर क्लिक करने के बाद, आपको नीचे दिए गए आउटपुट में अलर्ट मैसेज मिलेगा:
