garethhall Posted May 28, 2009 Share Posted May 28, 2009 Hello I have the code below and all works great but my name and phone filter doesn't seem to do a very good job. If you place number in the middle of a name it still validate and the same goes for the phone number but with letter in the middle? // Validation Filters var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; var nameReg = /[A-Za-z\'\-]/; var phoneReg = /[0-9\(\)\-\+ ]{7,16}/; function liveValidation(frmName, fieldName, fieldWhat, apprvMK, rejectMK){ var varReg; if(fieldWhat == "email"){ varReg = emailReg; }else if (fieldWhat == "name"){ varReg = nameReg; }else{ varReg = phoneReg; } var fieldVar = document.forms[frmName].elements[fieldName].value; var fieldVarName = document.forms[frmName].elements[fieldName].name if(!fieldVar.match(varReg)){ document.getElementById("spn_"+fieldVarName).innerHTML = "<img src='"+rejectMK+"' width='12' height='12' />"; }else{ document.getElementById("spn_"+fieldVarName).innerHTML = "<img src='"+apprvMK+"' width='12' height='12' />"; } } Link to comment https://forums.phpfreaks.com/topic/159971-solved-regular-expression-problem/ Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 I'm going to assume a standard phone number is 10-digits long - (xxx) xxx-xxxx or xxx-xxx-xxxx. var nameReg = /^[a-z]+$/; var phoneReg = /^(\(\d{3}\)|\d{3})\s?\-?\d{3}\-?\d{4}$/; Link to comment https://forums.phpfreaks.com/topic/159971-solved-regular-expression-problem/#findComment-843877 Share on other sites More sharing options...
garethhall Posted May 28, 2009 Author Share Posted May 28, 2009 Thank you so much for you help ^ +$ is what I needed. the nameReg is worg great now And I updated the phone one to but not with your reg but to this var phoneReg = /^[0-9\(\)\-\+ ]{7,16}$/; This is great Thanks Link to comment https://forums.phpfreaks.com/topic/159971-solved-regular-expression-problem/#findComment-844462 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.