I have a simple validation script. The first line of the validation is:
function Validate()
{
if (document.form.name.value == '')
{
the id of my form is form, so my query is will this validation work, as it does not have a name but it has an id. Should I change the name (above) to id?
Form validation of the pattern if (document.form1.username.value == '') is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
Sorry I do not fully understand you, are you saying that my code is not going to work? I am not a regular member of this forum, so would not know if the validation has been covered many times either. I could delve into regular expressions but it is just for a simple form, and so would work OK I feel. with fields of name email and message.
I usually do something along these lines for my client side validation. Instead of alerting the errors, another option is to highlight the input labels in the form with a different colour, typically red.
Code:
function validateForm(){
var inp1 = document.getElementById('txtInp1').value;
//... similarly for other inputs to be validated
var errMsg = "The following inputs are invalid:\n\n"
var errors = new Array();
var isDataValid = true;
//validate inp1
if(/[^0-9]/.test(inp1)){
isDataValid = false;
errors.push('inp1');
}
//validate the other inputs
//...
//display any error messages
if(!isDataValid){
for(i=0; i < errors.length; i++){
errMsg += errors[i] + "\n";
}
alert(errMsg);
}
return isDataValid;
}