NaniG Posted June 13, 2011 Share Posted June 13, 2011 Hi to all, how can, show and hide the textbox depending on the selection of the radio buttons and validating the textbox. <script language="javascript"> function showHide(value) { if(value=='show') { document.getElementById('emailid').style.display=''; } else { document.getElementById('emailid').style.display='none'; } if(value=='hide') { document.getElementById('emailid').style.display='none'; } else { document.getElementById('emailid').style.display=''; } } </script> <input type="radio" id="selectemailid" name="emailid" value="<?php echo $_POST['email'] ?>" class="txtbox" checked="checked" onclick="showHide('hide')"> <input type="radio" id="selectemailid" name="emailid" class="txtbox" onclick="showHide('show')"> <input type="text" name="email" id="emailid" class="txtbox" size="30" style="display:none;"> <input type="submit" name="continue" value="Continue" class="txt12" onClick="javascript:valbutton(this.form);"> Please how to validate the textbox? Quote Link to comment https://forums.phpfreaks.com/topic/239258-textbox-validation/ Share on other sites More sharing options...
fugix Posted June 13, 2011 Share Posted June 13, 2011 is the value of the textbox being stored in a database? Also, i would recommend that you use php for any form validation, as if using javascript. the user can disable it and tamper with your data. Quote Link to comment https://forums.phpfreaks.com/topic/239258-textbox-validation/#findComment-1229172 Share on other sites More sharing options...
NaniG Posted June 13, 2011 Author Share Posted June 13, 2011 Yes, it is stored in the database. The first field shows the email address, i have given an option to change the email address. If the end user choose radio button (others) then it shows a textbox. I want to validate this textbox so that the textbox would not be empty field. Any idea please....! Quote Link to comment https://forums.phpfreaks.com/topic/239258-textbox-validation/#findComment-1229174 Share on other sites More sharing options...
fugix Posted June 13, 2011 Share Posted June 13, 2011 let me make sure that i am understanding you correctly, you want to make it so the user cannot submit the form without the textbox being fiiled out? Quote Link to comment https://forums.phpfreaks.com/topic/239258-textbox-validation/#findComment-1229185 Share on other sites More sharing options...
NaniG Posted June 13, 2011 Author Share Posted June 13, 2011 Yes, the end user must have to filled with email address if he selects 'others'. Quote Link to comment https://forums.phpfreaks.com/topic/239258-textbox-validation/#findComment-1229195 Share on other sites More sharing options...
fugix Posted June 13, 2011 Share Posted June 13, 2011 to put this very basically, you will want to direct you form action to a php page, then on that page you will grab your text box field $email = $_POST['email']; $submit = $_POST['continue']; then check is your submit button has been clicked, and if the textbox has been filled out. if(isset($submit)) { if(!empty($email)) { //proceed from here } } from here you can filter the user input, etc... Quote Link to comment https://forums.phpfreaks.com/topic/239258-textbox-validation/#findComment-1229202 Share on other sites More sharing options...
NaniG Posted June 14, 2011 Author Share Posted June 14, 2011 Is it possible to validate in javascript? if it is yes, how? Quote Link to comment https://forums.phpfreaks.com/topic/239258-textbox-validation/#findComment-1229495 Share on other sites More sharing options...
fugix Posted June 14, 2011 Share Posted June 14, 2011 Is it possible to validate in javascript? if it is yes, how? yes, however part of validation is filtering user information, which can be done in JS but is not recommended. Since javascript can be disabled via browser settings, which disables your validation and allows the user to pass any information that he/she wants and exploits your script to SQL injection...i strongly recommend using php to validate all user input Quote Link to comment https://forums.phpfreaks.com/topic/239258-textbox-validation/#findComment-1229501 Share on other sites More sharing options...
KevinM1 Posted June 14, 2011 Share Posted June 14, 2011 The best validation strategy is to use JavaScript for responsiveness and to give the user feedback immediately, while using the back end language (likely PHP in this case) for security. That said, yes, you can use JavaScript to validate a textarea and stop a form from submitting it the textarea doesn't include any text. Just realize that this validation does nothing to secure your site. It's simply a user interface nicety. My JavaScript is a bit rusty, but try something along these lines: var form = document.forms["myForm"]; // substitute 'myForm' with whatever your form name actually is form.onsubmit = function() { var re = /.+/; var textExists = re.match(this.elements["textarea"].value); // replace 'textarea' with whatever your textarea's name actually is if(!textExists) { // highlight the textarea? return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/239258-textbox-validation/#findComment-1229520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.