anth0ny Posted December 18, 2008 Share Posted December 18, 2008 Hi everyone, I have been searching around the net but couldnt find an answer to this. How would i validate this form field that uses the value field for onfocus and onblur? Its not empty as it has 'i.e ITA001' in the field. I need something that says, if 'i.e ITA001' show error type of thing <input name="customerRef" type="text" class="fields" id="customerRefField" onfocus="if (value='i.e ITA001') {value=''}" onblur="if (value=='') {value='i.e ITA001'}" value="i.e ITA001" size="25" /> thanks for your help everyone Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/ Share on other sites More sharing options...
ngreenwood6 Posted December 18, 2008 Share Posted December 18, 2008 You should be posting this in the ajax forum. Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718732 Share on other sites More sharing options...
cytech Posted December 18, 2008 Share Posted December 18, 2008 <input name="customerRef" type="text" class="fields" id="customerRefField" onFocus="if(this.value=='i.e ITA001'){ this.value='';}" onBlur="if(this.value==''){this.value='i.e ITA001';}" value="i.e ITA001" size="25" /> Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718736 Share on other sites More sharing options...
Adam Posted December 18, 2008 Share Posted December 18, 2008 You mean better suited in the JS forum? Better off using a function: function defaultValSwitch(obj, default, e) { var val; if (e == 'onfocus') { val = (obj.value == default) ? '' : obj.value; } else { val = (obj.value == '') ? default : obj.value; } obj.value = val; return; } Then you'd call it like: <input name="customerRef" type="text" class="fields" id="customerRefField" onfocus="defaultValSwitch(this, 'i.e ITA001', event);" onblur="defaultValSwitch(this, 'i.e ITA001', event);" value="i.e ITA001" size="25" /> I haven't tested this but it should work - just going off memory! EDIT: I'd put if == 'onclick' instead of 'onfocus' .. A Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718746 Share on other sites More sharing options...
anth0ny Posted December 18, 2008 Author Share Posted December 18, 2008 Not sure if i've done something wrong but getting an error: object expected? Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718758 Share on other sites More sharing options...
Adam Posted December 18, 2008 Share Posted December 18, 2008 Post the code as you have it... A Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718777 Share on other sites More sharing options...
anth0ny Posted December 18, 2008 Author Share Posted December 18, 2008 cheers appreciated <script type="text/javascript"> <!-- function defaultValSwitch(obj, default, e) { var val; if (e == 'onclick') { val = (obj.value == default) ? '' : obj.value; } else { val = (obj.value == '') ? default : obj.value; } obj.value = val; return; } //--> </script> <input name="customerRef" type="text" class="fields" id="customerRefField" onclick="defaultValSwitch(this, 'i.e ITA001', event);" onblur="defaultValSwitch(this, 'i.e ITA001', event);" value="i.e ITA001" size="25" /> Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718783 Share on other sites More sharing options...
Adam Posted December 18, 2008 Share Posted December 18, 2008 Try this: <script type="text/javascript"> <!-- function defaultValSwitch(obj, default, e) { var val; if (e == 'onfocus') { val = (obj.value == default) ? '' : obj.value; } else { val = (obj.value == '') ? default : obj.value; } obj.value = val; return; } //--> </script> <input name="customerRef" type="text" class="fields" id="customerRefField" onfocus="defaultValSwitch(this, 'i.e ITA001', 'onfocus');" onblur="defaultValSwitch(this, 'i.e ITA001', 'onblur');" value="i.e ITA001" size="25" /> Note that I've switched onclick to onfocus! A Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718801 Share on other sites More sharing options...
anth0ny Posted December 18, 2008 Author Share Posted December 18, 2008 getting an error: expected identifier? any ideas cheers Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718815 Share on other sites More sharing options...
Adam Posted December 18, 2008 Share Posted December 18, 2008 Sorry my bad, I didn't realize 'default' was a reserved name! <script type="text/javascript"> <!-- function defaultValSwitch(obj, def, e) { var val; if (e == 'onfocus') { val = (obj.value == def) ? '' : obj.value; } else { val = (obj.value == '') ? def : obj.value; } obj.value = val; return; } //--> </script> A Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718833 Share on other sites More sharing options...
anth0ny Posted December 18, 2008 Author Share Posted December 18, 2008 ok its not throwing any errors now, but i still get 'i.e ITA001' inserted into the database Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718859 Share on other sites More sharing options...
cytech Posted December 18, 2008 Share Posted December 18, 2008 Hey anthony, Into the database? sounds like you wanted this if statement to check the field before it was added? You would need to do this via php and not javascript. if($_POST['customerRef'] == "i.e ITA001"){ $_POST['customRef'] = ""; } Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718863 Share on other sites More sharing options...
Adam Posted December 18, 2008 Share Posted December 18, 2008 Waah? Database? Sorry I had no idea this was what you were trying to achieve. I simply rewrote the code you had in the onfocus and onblur attributes... As cytech says.. Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718869 Share on other sites More sharing options...
anth0ny Posted December 18, 2008 Author Share Posted December 18, 2008 no worries cheers for the help, how would intergrate the php into my script, have tried but cant get it to work if(isset($_POST['save'])) { $departureDate = date("Y-m-d", strtotime($_POST['departureDate'])); $expireDate = date("Y-m-d", strtotime($_POST['expireDate'])); $airport = $_POST['airport']; $resort = $_POST['resort']; $hotel = $_POST['hotel']; $duration = $_POST['duration']; $board = $_POST['board']; $price = $_POST['price']; $specialoffer = $_POST['specialoffer']; $description = $_POST['description']; $customerRef = $_POST['customerRef']; $mystiqueRef = $_POST['mystiqueRef']; $stars = $_POST['stars']; if(!get_magic_quotes_gpc()) { $airport = addslashes($airport); $resort = addslashes($resort); $hotel = addslashes($hotel); $duration = addslashes($duration); $board = addslashes($board); $price = addslashes($price); $specialoffer = addslashes($specialoffer); $description = addslashes($description); $customerRef = addslashes($customerRef); $mystiqueRef = addslashes($mystiqueRef); $stars = addslashes($stars); } include 'library/config.php'; include 'library/opendb.php'; $query = "INSERT INTO test (departureDate, expireDate, airport, destination, resort, hotel, duration, board, price, specialoffer, description, customerRef, mystiqueRef, stars) VALUES ('$departureDate', '$expireDate', '$airport', '$destination', '$resort', '$hotel', '$duration', '$board', '$price', '$specialoffer', '$description', '$customerRef', '$mystiqueRef', '$stars')"; Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718884 Share on other sites More sharing options...
cytech Posted December 18, 2008 Share Posted December 18, 2008 Where you have: $customerRef = $_POST['customerRef']; Put if($_POST['customerRef']=="e.i AIT001"){ $customerRef = ""; }else{ $customerRef = $_POST['customerRef']; } On a side note you should use mysql_real_escape_string() instead of addslashes, it is much better Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718885 Share on other sites More sharing options...
anth0ny Posted December 18, 2008 Author Share Posted December 18, 2008 thank you works perfectly, really appreciate the help and support here Link to comment https://forums.phpfreaks.com/topic/137533-solved-form-validation/#findComment-718891 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.