I am not taking any course on this, and I would greatly appreciate if you can divulge a little of the modified code to me. In this way, I won’t be totally stuck in this situation, and I would be able to modify the code clearly.
I should clarify for you that I have no modified code for this. If you want me to write the code for you at every step, so that at the end of it I wind up writing all of the code for you, so that your script does exactly what you want it to, I can tell you right now that that is not going to happen.
There are people who are normally paid to do that sort of work. Not only do I feel that I’m taking bread from their collective mouths, but I also end up feeling taken advantage myself, because my efforts won’t have ended up with you learning much at all.
I can and will instead help you to learn about JavaScript and the theory behind things that you don’t understand, so that you can learn enough to enable yourself to solve these types of situations for yourself.
No, I’m not asking you to write every code for me. I just need you to write a small part of the code so that I know how to start the whole modification. I seriously have no idea what to change in ValidateForm function.
The code in post #18 are all examples of different ways that you would use, within the ValidateForm function.
YESSSSSSSSS I KNOWWWWW.
But I don’t even know which part should I put them in ValidateForm!
Let us go to an even simpler level then.
Say you have a function that does stuff (we don’t need to know what) and returns true if it succeeds, or false if it fails.
function someFunc(someValue) {
// do stuff
...
if (...) {
// it wasn't good, so return false
return false;
}
// nothing bad seemed to happen, so return true.
return true;
}
That is essentially what the ValidatePhone function does. Currently it doesn’t matter how it does it, just that it returns a certain value depending on whether it’s successful or not.
The ValidatePhone function returns false when validation for a certain field fails, and it returns true when validation is successful.
What you are wanting to do from the ValidateForm function is to record that returned result, so that you can then find out what that result is, and take some action, based on that result.
For example:
var isGood = someFunc(someValue);
Now you can check some conditions based on that returned value, and take appropriate action.
if (isGood === false) {
return false;
}
Do you grasp the type of logic that is required here?
:mad:I don’t understand at all.
We may have to flag this as a lost cause then.
Do you mean like this?
function ValidateForm()
{
ValidatePhone(document.getElementById("Text2"));
ValidatePhone(document.getElementById("Text3"));
var isValid = ValidatePhone(Phone);
if (isValid == false)
{
ValidatePhone(document.getElementById("Text1"));
return false;
}
}
Nearly. Move the lines with text2 and text3 to the end of the function, and replace the ValidatePhone(Phone) part with the text1 line instead.
I don’t understand again. Is it like this?
function ValidateForm()
{
var isValid = (document.getElementById("Text1"));
if (isValid == false)
{
ValidatePhone(document.getElementById("Text1"));
return false;
}
ValidatePhone(document.getElementById("Text2"));
ValidatePhone(document.getElementById("Text3"));
}
Close, that text1 line inside the if statement doesn’t need to be there. Once that goes after the isValid equal sign, you will see that the code is getting the result from the first ValidatePhone function, and only if it it good will the code carry on to the other ValidatePhone functions.
You can also neaten the text1 ValidatePhone function line by removing the parenthesis from around it.
You will want to apply the same structure that you have for checking the result from the text1 line, to the text2 line as well.
Is it like this?
function ValidateForm()
{
var isValid = document.getElementById("Text1");
if (isValid == false)
{
return false;
}
var isValidforTwo = document.getElementById("Text2");
if (isValidforTwo == false)
{
return false;
}
var isValidforThree = document.getElementById("Text3");
if (isValidforThree == false)
{
return false;
}
}
Just wrap the document.getElementById parts with the ValidatePhone function.
You won’t need to keep separate isValid values either. You can reuse the same isValid variable throughout.
You mean like this?
function ValidateForm()
{
var isValid = document.getElementById(ValidatePhone(Phone));
if (isValid == false)
{
return false;
}
var isValid = document.getElementById(ValidatePhone(Phone));
if (isValid == false)
{
return false;
}
var isValid = document.getElementById(ValidatePhone(Phone));
if (isValid == false)
{
return false;
}
}
But if I do not declare different isValid, the system will say that it has been used.
The other way around.
For example:
var someValue = someFunc(document.getElementById(‘someId’));
Is it like this?
function ValidateForm()
{
var isValid = ValidatePhone(document.getElementById("Text1"));
if (isValid == false)
{
return false;
}
isValid = ValidatePhone(document.getElementById("Text2"));
if (isValid == false)
{
return false;
}
isValid = ValidatePhone(document.getElementById("Text3"));
if (isValid == false)
{
return false;
}
}
That’s right.
Do you see how each ValidatePhone function is being checked for failure, so that execution doesn’t carry on unless it returns something that is not false?
If you compare the code to this example from earlier on, it might be easier to understand now.
var isValid = someFunc();
if (isValid === false) {
return;
}
There’s a problem though. If I do not fill in anything into the textboxes and validates it, it shows an alert message, which is correct. But what I want is to show which textboxes have not been filled.
And if I manage to fill in the valid number in Text1 & Text2, it will show two alert messages of “YAY!”.
You can have the alert message show which field it is, by getting the name of the field.
I believe that your form only used id attributes right now, so you can get that name from inside the ValidatePhone function.
From inside the ValidatePhone function, you can use Phone.id to get the name of the identifier of the field that’s being checked. You could then make use of that in the alert message itself.