i'm writing javascript validator
and i change input font color when the input is incorrect
but i want this input font color to be changed back when the input is correct
i cannot do eg -
var email1= form.email1.value;
-
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
-
if (!filter.test(email1)) {
-
document.getElementById("email1").style.color="red";
-
if(language=="polish"){
-
form.email1.value="***Proszę wpisać poprawny email!";}
-
else {form.email1.value="***Please enter the right email!";}
-
document.getElementById("email1").style.color="black"; <--change back
-
return false;
-
}
-
because even when the field has incorrect value the font color is black instead of red
thank You
15 10042
i change input font color when the input is incorrect but i want this input font color to be changed back when the input is correct
ah the beauty if the if/else statement. I think your polish language statement might be getting in the way..... you're basically saying if the email is invalid, set it to red, check the language & give the user a message... then set it back to black. it does all this in the blind of an eye.
Also, a message from a potential user filling out your form: "if i made a mistake on my email, I don't want to have it replaced with an error message, I want to see an error message next to the input".... anyway, i've replaced that part of it with a "thrower" function, which you can write yourself -- and can include the language statements
try this: -
var email1= form.email1.value;
-
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
-
-
if (!filter.test(email1)) {
-
document.getElementById("email1").style.color="red";
-
thrower("***Please enter the right email!");
-
} else {
-
document.getElementById("email1").style.color="black";
-
}
-
i tried this why but then the form goes to the action file i mean that the return false doesnt work
hsriat 1,654
Recognized Expert Top Contributor -
var email1= form.email1.value;
-
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
-
if (!filter.test(email1)) {
-
document.getElementById("email1").style.color="red";
-
if(language=="polish")
-
form.email1.value="***Proszę wpisać poprawny email!";
-
else
-
form.email1.value="***Please enter the right email!";
-
return false;
-
}
-
else {
-
document.getElementById("email1").style.color="black"; <--change back
-
return true;
-
}
-
There's nothing wrong with the polish statement.
In line number 6 and 8, I think you may want to use alert, but I left the way it was.
but in my validator i have lots of other fields to check and if i write return true(when checking first field) it will go to the action file and send it to database
hsriat 1,654
Recognized Expert Top Contributor
but in my validator i have lots of other fields to check and if i write return true(when checking first field) it will go to the action file and send it to database
Then don't return true there, but do it in the end of the function after applying all the validations.
can i do document.getElementById("email1").style.color=="re d"; and then change it in to black if not how do i check what is the font color of input
hsriat 1,654
Recognized Expert Top Contributor
can i do document.getElementById("email1").style.color=="re d"; and then change it in to black if not how do i check what is the font color of input
I guess there's something wrong with your algorithm, can you please your code here.
hmm it's a bit loooong
[code=javascript]
function checkForm(form,number,language,companyNames,emails ){
var email1= form.email1.value;
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email1)) {
document.getElementById("email1").style.color="red ";
if(language=="polish"){
form.email1.value="***Proszę wpisać poprawny email!";}
else {form.email1.value="***Please enter the right email!";}
return false;
}
for(var i=0;i<emails.length;i++){
if(form.email1.value==emails[i]){
document.getElementById("email1").style.color="red ";
if(language=="polish") {form.email1.value = "***Podany email jest już w bazie danych!";}
else {form.email1.value = "***Email already exists in our database!";}
break;
return false;}
}
var email2= form.email2.value;
if (!filter.test(email2)) {
document.getElementById("email2").style.color="red ";
if(language=="polish"){
form.email2.value="***Proszę wpisać poprawny email!";}
else {form.email2.value="***Please enter the right email!";}
return false;
}
if(document.getElementById("email2").style.color== "red")
document.getElementById("email2").style.color=="bl ack";
if(!form.email1.value==form.email2.value){
document.getElementById("email2").style.color="red ";
if(language=="polish"){
form.email2.value="***Proszę wpisać poprawny email!";}
else { form.email2.value="***Please enter the right email!";}
return false;
}
if(form.email1.value.length==0 || form.email1.value=="***Proszę wpisać email!" || form.email1.value=="***Please enter email!"){
document.getElementById("email1").style.color="red ";
if(language=="polish"){
form.email1.value="***Proszę wpisać email!";}
else {form.email1.value="***Please enter email!";}
return false;
}
if(form.email2.value.length==0 || form.email2.value=="***Proszę wpisać email!" || form.email2.value=="***Please enter email!"){
document.getElementById("email2").style.color="red ";
if(language=="polish"){
form.email2.value="***Proszę wpisać email!";}
else {form.email2.value="***Please enter email!";}
return false;}
if(document.getElementById("email2").style.color== "red")
document.getElementById("email2").style.color=="bl ack";
if (form.password1.value.length==0 ||form.password1.value.length<8) {
document.getElementById("password1").style.border= "solid red 2px";
return false;
}
if(document.getElementById("password1").style.bord er=="solid red 2px")
document.getElementById("password1").style.border= ="solid #6480A4 1px";
if (form.password2.value.length==0 ||form.password2.value.length<8) {
document.getElementById("password2").style.border= "solid red 2px";
return false;
}
if(form.password1.value!=form.password2.value){
document.getElementById("password2").style.border= "solid red 2px";
return false;
}
if(form.name.value.length==0 || form.name.value=="***Proszę wpisać Imię i Nazwisko!" || form.name.value=="***Please enter firstname and lastname!" ){
document.getElementById("name").style.color="red";
if(language=="polish"){
form.name.value="***Proszę wpisać Imię i Nazwisko!";}
else {form.name.value="***Please enter firstname and lastname!";}
return false;
}
if(!IsNumeric(form.phone.value)){
document.getElementById("phone").style.color="red" ;
if(language=="polish"){
form.phone.value="***Proszę wpisać poprawny numer telefonu!";}
else {form.phone.value="***Please enter the right phone number!";}
return false;}
if(document.getElementById("phone").style.color==" red")
document.getElementById("phone").style.color=="bla ck";
if(form.phone.value.length==0 || form.phone.value=="***Prosze wpisać numer telefonu!" || form.phone.value=="***Please enter phone number!"){
document.getElementById("phone").style.color="red" ;
if(language=="polish"){
form.phone.value="***Proszę wpisać numer telefonu!";}
else {form.phone.value="***Please enter phone number!";}
return false;
}
for(var i=0;i<companyNames.length;i++){
if(form.company_name.value==companyNames[i]){
document.getElementById("company_name").style.colo r="red";
if(language=="polish") {form.company_name.value = "***Podana firma jest już zarejestrowana w bazie danych!";break;return false;}
else {form.company_name.value = "***Company name already exists in our database!";break;return false;}
}
}
if(form.company_name.value.length==0 || form.company_name.value=="***Proszę wpisać nazwę firmy!" || form.company_name.value=="***Please enter company name!"){
document.getElementById("company_name").style.colo r="red";
if(language=="polish"){
form.company_name.value="***Proszę wpisać nazwę firmy!";}
else {form.company_name.value="***Please enter company name!";}
return false;}
if(form.country.selectedIndex== 0){
hideShow('client_insert_alert_location');
return false;}
if(form.sector.selectedIndex== 0){
hideShow('client_insert_alert_sector');
return false;}
var images=new Array("AzBd1X","BdYC8o","Ug6iYp");
if(images[number]!=form.confirmation.value){
document.getElementById("confirmation").style.colo r="red";
if(language=="polish") {form.confirmation.value = "***Prosze wpisać poprawny kod!";}
else {form.confirmation.value = "***Please enter the right code!";}
return false;
}
return true;
}[code]
thank You
hsriat 1,654
Recognized Expert Top Contributor
Modify all the validations as I told you in my first post. Just don't write return true;.
Let that be in the end of the function as you have done.
when i add else statement it works only with the first field
thank You
hsriat 1,654
Recognized Expert Top Contributor
There might be some problem with your algorithm.
Keep the general algorithm, like this.. - Do Validation 1
-
If not valid {
-
turn color = red
-
if language is Polish {
-
show error in Polish
-
}
-
else { //if language is not polish
-
show error in English
-
}
-
return false
-
else { //if valid
-
turn color = black
-
}
-
Do Validation 2
-
If not valid {
-
turn color = red
-
if language is Polish {
-
show error in Polish
-
}
-
else { //if language is not polish
-
show error in English
-
}
-
return false
-
else { //if valid
-
turn color = black
-
}
-
Do Validation 3
-
If not valid {
-
turn color = red
-
if language is Polish {
-
show error in Polish
-
}
-
else { //if language is not polish
-
show error in English
-
}
-
return false
-
else { //if valid
-
turn color = black
-
}
-
//do all validations like this
-
-
return true;
Suggestion: Format your code properly so that in case there's any bug, its easily traceable.
thank You
it works now :)
hsriat 1,654
Recognized Expert Top Contributor
thank You
it works now :)
Glad to know that. :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Eric Pickup |
last post by:
I'm having a problem with the following html (I've simplified it down
to a simple test case). Could anyone point out why Mozilla is having
problems with this (I've tried so many variations that I...
|
by: David. E. Goble |
last post by:
Hi All;
I have a few of these;
sigsImages="sigs/finished1.jpg"
sigsImages="sigs/foghorn.jpg"
lower I have;
|
by: davidkarlsson74 |
last post by:
Error: document.getElementById("folderMenu").cells has no properties
File: http://www.volkswagen.se/tillbehor/js/foldermenu.js
Rad: 49
The function activates different DIV:s, but doesn't seem to...
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - Why doesn't the global variable "divId" always refer to the element with id="divId"?...
|
by: rkhurana |
last post by:
Hi
I am writing a JSF application that uses some third party charts. While I can see the chart but the javascript that is supposed to calla function to update chart periodically has a problem. It...
|
by: msg2ajay |
last post by:
hi,
i am working on <div> i have to hide some part of the table. I am not able to hide that table part can anybady tell me where is the error.
<html>
<head>
<script...
|
by: karprix |
last post by:
Hi, I am working on a webapplication,( with Ajax & JSF) where I want to retrieve the value present in a <t:inputText id="starttimeScheduler" value="#{scheduledJobCreateEdit.startTimeDate}"...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |