PDA

View Full Version : Check if textbox2 is empty when textbox1 is not



freddd
07-15-2005, 02:00 PM
One more validation question from me.
I have this code which validates only the form fields that is NOT empty. The validation occurs only when there is some data in the textboxes.




<script language="JavaScript">
function checkInput(){

if (document.form2.textbox1.value.length < 4){
alert("4");
return (false);
}
if (document.form2.textbox2.value.length < 4){
alert("4");
return (false);
}
if (document.form2.textbox3.value.length < 6){
alert("6");
return (false);
}
if (document.form2.textbox4.value.length < 6){
alert("6");
return (false);
}


}
</script>

What I need to do is to add one more check before the data is submited. If textbox1 has some data then textbox2 also need to have some data. So if the useres choose to type in for instance start number in textbox1 he must be forced to enter the stop number in textbox2. Same goes for textbox3 and textbox4.

Thanks

nikkiH
07-15-2005, 03:08 PM
So all spaces is an okay value to have?
Or all underscores?

You're only checking length, not what characters make up that length. Is that all right?
You may want to use a regular expression instead to constrain the values.
Something like these.
http://www.ipwebdesign.net/kaelisSpace/useful_jsValidation.html

As to the other thing, just do a compound IF.
Something like


if ( ! isBlank(document.form2.textbox1.value) &&
isBlank(document.form2.textbox2.value))
alert("Gotta enter that");
return (false);
}


Note: isBlank is a function found at that link that checks for blank fields

Bill Posters
07-15-2005, 03:12 PM


if (((document.form2.textbox1.value == "") && (document.form2.textbox2.value != "")) || ((document.form2.textbox1.value != "") && (document.form2.textbox2.value == ""))) {
alert("blah blah");
return (false);
}

See: Logical operators (http://www.w3schools.com/js/js_operators.asp)

vorl
07-21-2005, 10:58 AM
hi
sorry to ask another question but how would I use this on my form?
im not very good when it comes down to javascript!!

thanks vorl

Bill Posters
07-21-2005, 11:07 AM
No worries. You just need to slip it into your existing set of checks.

e.g.

<script type="text/javascript">

function checkInput() {

if (document.form2.textbox1.value.length < 4) {
alert("4");
return (false);
}

if (document.form2.textbox2.value.length < 4) {
alert("4");
return (false);
}

if (document.form2.textbox3.value.length < 6) {
alert("6");
return (false);
}

if (document.form2.textbox4.value.length < 6) {
alert("6");
return (false);
}

if (((document.form2.textbox1.value == "") && (document.form2.textbox2.value != "")) || ((document.form2.textbox1.value != "") && (document.form2.textbox2.value == ""))) {
alert("blah blah");
return (false);
}

}
</script>

vorl
07-21-2005, 01:20 PM
thanks Bill Posters

I was wondering if it was possible to put this code into another validation code i have??
here it is:



function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("FirstName", "LastName");
// Enter field description to appear in the dialog box
var fieldDescription = Array("First Name", "Last Name");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}

if (alertMsg.length == l_Msg){
submitonce(formobj);
return true;
}else{
alert(alertMsg);
return false;
}
}
// -->
</script>


thanks
vorl