creating a mandatory field in ui page?

caffry
Kilo Guru

i have a field which should have mandatory symbol (*) and when i enter the values it should change from red to grey..

<label>Name : </label>

<input type="text" ng-model="name" id="demo"  />

can i use it for the above the code...without using jelly script

1 ACCEPTED SOLUTION

Here is the full code

HTML

<label for="input1" id="inputLabel" >Name</label>
  <input type="text" ng-model="name" onchange="checkMandatory()" id="demo"  />

 

Client script

document.getElementById('inputLabel').setAttribute("class", "required");

function checkMandatory(){
if(document.getElementById('demo')!="")
document.getElementById('inputLabel').setAttribute("class", "notrequired");

}

css

label.required::before {
  content: '*';
  margin-right: 4px;
  color: red;
}
label.notrequired::before {
  content: '*';
  margin-right: 4px;
  color: grey;
}

 

View solution in original post

26 REPLIES 26

sorry for that while copying in community i forgot to change but

the * still remains red it didn't change the color when it is entered

find_real_file.png

Here is the full code

HTML

<label for="input1" id="inputLabel" >Name</label>
  <input type="text" ng-model="name" onchange="checkMandatory()" id="demo"  />

 

Client script

document.getElementById('inputLabel').setAttribute("class", "required");

function checkMandatory(){
if(document.getElementById('demo')!="")
document.getElementById('inputLabel').setAttribute("class", "notrequired");

}

css

label.required::before {
  content: '*';
  margin-right: 4px;
  color: red;
}
label.notrequired::before {
  content: '*';
  margin-right: 4px;
  color: grey;
}

 

it worked

gel('inputLabel').setAttribute("class", "required");
function checkMandatory(){
if(gel('demo').value!="")
{
	gel('inputLabel').setAttribute("class", "notrequired");
}
	else
		{
			gel('inputLabel').setAttribute("class", "required");
		}
}

 

can we modify that like

when we have not entered the field the  form should not submit that too should be based on the mandatory

 

can u please help me out

You can evaluate if the field has value or not  in onclick function of the button which you are using to submit this record.

SANKP
Kilo Contributor

Please help me in resolving this.

 

https://community.servicenow.com/community?id=community_question&sys_id=e794be6adb2d44106064eeb5ca96...

 

Making field Visible & Mandatory based on Other Field Value in UI Page