or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

credit-card-validation.mdemail-validation.mdform-control-utilities.mdindex.mdpassword-validation.mdtemplate-driven-forms.mduniversal-validation.md

universal-validation.mddocs/

0

# Universal Validation

1

2

Common data validation rules for whitespace, numeric ranges, string lengths, dates, and data types. These validators work with any form control and data type.

3

4

## Capabilities

5

6

### Whitespace Validation

7

8

Validates that the control value contains no whitespace characters.

9

10

```typescript { .api }

11

/**

12

* Validates that control value contains no whitespace

13

* @param control - AbstractControl to validate

14

* @returns ValidationErrors if whitespace found, null if valid

15

*/

16

static noWhitespace(control: AbstractControl): ValidationErrors | null;

17

```

18

19

**Usage Example:**

20

21

```typescript

22

import { UniversalValidators } from "ngx-validators";

23

import { FormControl } from "@angular/forms";

24

25

const control = new FormControl('', UniversalValidators.noWhitespace);

26

27

// Valid: "username", "test123", "no-spaces"

28

// Invalid: "user name", "test 123", " username"

29

```

30

31

**Error Object:**

32

```typescript

33

{

34

noWhitespaceRequired: true

35

}

36

```

37

38

### Empty String Validation

39

40

Validates that the control value is not an empty string after trimming whitespace.

41

42

```typescript { .api }

43

/**

44

* Validates that control value is not empty after trimming

45

* @param control - AbstractControl to validate

46

* @returns ValidationErrors if empty, null if valid

47

*/

48

static noEmptyString(control: AbstractControl): ValidationErrors | null;

49

```

50

51

**Usage Example:**

52

53

```typescript

54

const control = new FormControl('', UniversalValidators.noEmptyString);

55

56

// Valid: "content", "a", "123"

57

// Invalid: "", " ", "\t\n"

58

```

59

60

**Error Object:**

61

```typescript

62

{

63

noEmptyString: true

64

}

65

```

66

67

### Number Validation

68

69

Validates that the control value is a valid number.

70

71

```typescript { .api }

72

/**

73

* Validates that control value is a valid number

74

* @param control - AbstractControl to validate

75

* @returns ValidationErrors if not a number, null if valid

76

*/

77

static isNumber(control: AbstractControl): ValidationErrors | null;

78

```

79

80

**Usage Example:**

81

82

```typescript

83

const control = new FormControl('', UniversalValidators.isNumber);

84

85

// Valid: 123, "456", 78.9, "0.5"

86

// Invalid: "abc", "12abc", "", null

87

```

88

89

**Error Object:**

90

```typescript

91

{

92

numberRequired: true

93

}

94

```

95

96

### Range Validation

97

98

Validates that a numeric value falls within a specified range (inclusive).

99

100

```typescript { .api }

101

/**

102

* Validates that numeric value is within specified range

103

* @param minValue - Minimum allowed value (inclusive)

104

* @param maxValue - Maximum allowed value (inclusive)

105

* @returns ValidatorFn that validates numeric range

106

*/

107

static isInRange(minValue: number, maxValue: number): ValidatorFn;

108

```

109

110

**Usage Example:**

111

112

```typescript

113

// Age must be between 18 and 65

114

const control = new FormControl('', UniversalValidators.isInRange(18, 65));

115

116

// Valid: 18, 25, 65

117

// Invalid: 17, 66, "abc"

118

```

119

120

**Error Objects:**

121

```typescript

122

// Value too small

123

{

124

rangeValueToSmall: {

125

requiredMinValue: number,

126

requiredMaxValue: number,

127

actual: any

128

}

129

}

130

131

// Value too large

132

{

133

rangeValueToBig: {

134

requiredMinValue: number,

135

requiredMaxValue: number,

136

actual: any

137

}

138

}

139

140

// Not a number

141

{

142

numberRequired: true

143

}

144

```

145

146

### String Length Validation

147

148

#### Minimum Length

149

150

Validates that string has at least the specified number of characters.

151

152

```typescript { .api }

153

/**

154

* Validates minimum string length

155

* @param minLength - Minimum required length

156

* @returns ValidatorFn that validates minimum length

157

*/

158

static minLength(minLength: number): ValidatorFn;

159

```

160

161

**Usage Example:**

162

163

```typescript

164

// Username must be at least 3 characters

165

const control = new FormControl('', UniversalValidators.minLength(3));

166

167

// Valid: "abc", "username", "test123"

168

// Invalid: "", "ab", "x"

169

```

170

171

**Error Object:**

172

```typescript

173

{

174

minLength: {

175

requiredMinLength: number,

176

actualLength: number

177

}

178

}

179

```

180

181

#### Maximum Length

182

183

Validates that string does not exceed the specified number of characters.

184

185

```typescript { .api }

186

/**

187

* Validates maximum string length

188

* @param maxLength - Maximum allowed length

189

* @returns ValidatorFn that validates maximum length

190

*/

191

static maxLength(maxLength: number): ValidatorFn;

192

```

193

194

**Usage Example:**

195

196

```typescript

197

// Username cannot exceed 20 characters

198

const control = new FormControl('', UniversalValidators.maxLength(20));

199

200

// Valid: "user", "username123", "a" * 20

201

// Invalid: "a" * 21, "verylongusernamethatexceedslimit"

202

```

203

204

**Error Object:**

205

```typescript

206

{

207

maxLength: {

208

requiredMaxLength: number,

209

actualLength: number

210

}

211

}

212

```

213

214

### Numeric Value Validation

215

216

#### Minimum Value

217

218

Validates that numeric value meets minimum threshold.

219

220

```typescript { .api }

221

/**

222

* Validates minimum numeric value

223

* @param min - Minimum allowed value

224

* @returns ValidatorFn that validates minimum value

225

*/

226

static min(min: number): ValidatorFn;

227

```

228

229

**Usage Example:**

230

231

```typescript

232

// Price must be at least $10

233

const control = new FormControl('', UniversalValidators.min(10));

234

235

// Valid: 10, 15.5, 100

236

// Invalid: 9, 0, -5

237

```

238

239

**Error Objects:**

240

```typescript

241

{

242

min: {

243

required: number,

244

actual: any

245

}

246

}

247

248

// If not a number

249

{

250

numberRequired: true

251

}

252

```

253

254

#### Maximum Value

255

256

Validates that numeric value does not exceed maximum threshold.

257

258

```typescript { .api }

259

/**

260

* Validates maximum numeric value

261

* @param max - Maximum allowed value

262

* @returns ValidatorFn that validates maximum value

263

*/

264

static max(max: number): ValidatorFn;

265

```

266

267

**Usage Example:**

268

269

```typescript

270

// Age cannot exceed 120

271

const control = new FormControl('', UniversalValidators.max(120));

272

273

// Valid: 25, 65, 120

274

// Invalid: 121, 200, 999

275

```

276

277

**Error Objects:**

278

```typescript

279

{

280

max: {

281

required: number,

282

actual: any

283

}

284

}

285

286

// If not a number

287

{

288

numberRequired: true

289

}

290

```

291

292

### Date Validation

293

294

#### Minimum Date

295

296

Validates that date value is not before the specified minimum date.

297

298

```typescript { .api }

299

/**

300

* Validates minimum date value

301

* @param minDate - Minimum allowed date

302

* @returns ValidatorFn that validates minimum date

303

*/

304

static minDate(minDate: Date): ValidatorFn;

305

```

306

307

**Usage Example:**

308

309

```typescript

310

// Birth date cannot be before 1900

311

const minBirthDate = new Date('1900-01-01');

312

const control = new FormControl('', UniversalValidators.minDate(minBirthDate));

313

314

// Valid: "1950-06-15", "2000-12-25"

315

// Invalid: "1899-12-31", "1800-01-01"

316

```

317

318

**Error Objects:**

319

```typescript

320

{

321

minDate: {

322

required: Date,

323

actual: Date

324

}

325

}

326

327

// If not a valid date

328

{

329

dateRequired: true

330

}

331

```

332

333

#### Maximum Date

334

335

Validates that date value is not after the specified maximum date.

336

337

```typescript { .api }

338

/**

339

* Validates maximum date value

340

* @param minDate - Maximum allowed date (parameter name is misleading but functional)

341

* @returns ValidatorFn that validates maximum date

342

*/

343

static maxDate(minDate: Date): ValidatorFn;

344

```

345

346

**Usage Example:**

347

348

```typescript

349

// Event date cannot be in the future

350

const today = new Date();

351

const control = new FormControl('', UniversalValidators.maxDate(today));

352

353

// Valid: yesterday's date, today's date

354

// Invalid: tomorrow's date, future dates

355

```

356

357

**Error Objects:**

358

```typescript

359

{

360

maxDate: {

361

required: Date,

362

actual: Date

363

}

364

}

365

366

// If not a valid date

367

{

368

dateRequired: true

369

}

370

```

371

372

### Type Validation

373

374

Validates that the control value matches the specified JavaScript type.

375

376

```typescript { .api }

377

/**

378

* Validates that control value matches specified type

379

* @param type - Required JavaScript type

380

* @returns ValidatorFn that validates data type

381

*/

382

static type(type: "number" | "string" | "object" | "boolean"): ValidatorFn;

383

```

384

385

**Usage Example:**

386

387

```typescript

388

// Ensure value is a string

389

const stringControl = new FormControl('', UniversalValidators.type('string'));

390

391

// Ensure value is a number

392

const numberControl = new FormControl('', UniversalValidators.type('number'));

393

394

// Ensure value is a boolean

395

const booleanControl = new FormControl('', UniversalValidators.type('boolean'));

396

397

// Ensure value is an object

398

const objectControl = new FormControl('', UniversalValidators.type('object'));

399

```

400

401

**Error Object:**

402

```typescript

403

{

404

type: {

405

required: string,

406

actual: string

407

}

408

}

409

```

410

411

## Template-Driven Forms Support

412

413

For template-driven forms, use the universal validator directives:

414

415

```html

416

<!-- No whitespace validation -->

417

<input

418

name="username"

419

[(ngModel)]="user.username"

420

#username="ngModel"

421

whiteSpaceValidator>

422

423

<!-- Empty string validation -->

424

<input

425

name="description"

426

[(ngModel)]="user.description"

427

#description="ngModel"

428

emptyStringValidator>

429

430

<!-- Number validation -->

431

<input

432

name="age"

433

[(ngModel)]="user.age"

434

#age="ngModel"

435

isNumberValidator>

436

437

<!-- Range validation -->

438

<input

439

name="score"

440

[(ngModel)]="user.score"

441

#score="ngModel"

442

[isInRangeValidator]="true"

443

[minValue]="0"

444

[maxValue]="100">

445

446

<!-- Min/Max value validation -->

447

<input

448

name="price"

449

[(ngModel)]="product.price"

450

#price="ngModel"

451

[minValidator]="0.01"

452

[maxValidator]="999.99">

453

454

<!-- Date validation -->

455

<input

456

type="date"

457

name="birthDate"

458

[(ngModel)]="user.birthDate"

459

#birthDate="ngModel"

460

[minDateValidator]="minBirthDate"

461

[maxDateValidator]="maxBirthDate">

462

463

<!-- Type validation -->

464

<input

465

name="value"

466

[(ngModel)]="data.value"

467

#value="ngModel"

468

[typeValidator]="'string'">

469

```

470

471

**Error Display Examples:**

472

473

```html

474

<div *ngIf="username.errors?.noWhitespaceRequired">

475

Username cannot contain spaces.

476

</div>

477

478

<div *ngIf="age.errors?.numberRequired">

479

Please enter a valid number.

480

</div>

481

482

<div *ngIf="score.errors?.rangeValueToSmall">

483

Score must be at least {{score.errors.rangeValueToSmall.requiredMinValue}}.

484

</div>

485

486

<div *ngIf="price.errors?.min">

487

Price must be at least ${{price.errors.min.required}}.

488

</div>

489

```