views:

34

answers:

2

i would like to display the error message in the input element itself instead of showing an error message separately. how would i capture the element in which the message needs to be displayed?

this the code:

function checkForm(form) { 

        if (yname.getValue() == "" || yname.getValue() == "Your Name" || name.getValue() == "" || name.getValue() == "Name of the Book" || url.getValue() == "" || url.getValue() == "URL of the Book"){
              [id-of-the-element].setTextValue("Field cannot be empty!");
             return false;
        }  
A: 

this is plain java script code if you are not using jquery

document.getElementById("id-of-the-element").value = "Field cannot be empty!";

yilmazhuseyin
+1  A: 

You can "extend" your condition statement:

function checkForm(form) {
    var result = true;
    if (yname.getValue() == "" || yname.getValue() == "Your Name") {
        setErrorMessage(yname);
        result = false;
    } else if (name.getValue() == "" || name.getValue() == "Name of the Book") {
        setErrorMessage(yname);
        result = false;
    } else if (url.getValue() == "" || url.getValue() == "URL of the Book") {
        setErrorMessage(yname);
        result = false;
    }
    return result;
}

function setErrorMessage(field) {
    field.setTextValue("Field cannot be empty!");
}
ZloiAdun
This is if I understood you correctly - you need to know which field is empty
ZloiAdun
i suppose there's no other way?
fuz3d
Of course it can be refactored in some way, for example (yname.getValue() == "" || yname.getValue() == "Your Name") can be moved into separate function.But there is no a standard function that you may call and it will give you what fields are empty. However there can be and probably somewhere it is, the function to get the empty fields of a form. But in your example your fields have some default values which also counts as an empty value.
ZloiAdun